Python 中文件的动态名称

dynamic name for a file in Python

我有一个subject_id,它是一个动态的number.For实例,它可能等于60。我手动定义了一些文件名如下: x_file = "50.txt" x_csv_file = "50.csv" 数字 (50) 可能是 1 或其他任何数字。有什么方法可以让我只定义一次 subject_id=50,然后将这些名称用作 x_file = "subject_id.txt"x_csv_file = "subject_id.csv"? 感谢您的帮助

您可能想为此定义一个简单的函数

def file_name(subject_id):
    x_file = '{}.txt'.format(subject_id)
    x_csv_file = '{}.csv'.format(subject_id)
    return x_file, x_csv_file