我如何 return os.path 的文件名作为字符串?

How do I return a filename for os.path as a string?

我正在遍历文件列表。对于每个文件,我想将文件名分配给字符串类型的变量。我想在打印语句中使用文件名字符串来命名输出文件和日志条目。

我尝试获取文件名,然后使用 str() 函数。

def source_data():
    # Create path and file listing for csv exports in landing zone
    csv_dir = 'C:\Path\to\data\nets'
    file_list = list(os.scandir(csv_dir))
    csv_file_list = list(filter(lambda x: x.path.endswith('.csv'), 
    file_list))
return csv_file_list  

for f in csv_file_list:
    with open(file=f, mode='r', newline='') as csvfile:
        filename = os.path.splitext(os.path.split(f)[1])[0]

....process loop

def net_file(n_string, filename):
    with open(file=filename + '.txt', mode='w') as txtfile:
        print(n_string, file=txtfile)
    print('Finished Processing.... {} Subnets...'.format(filename))

当我 运行 脚本时,出现此错误...

with open(file=filename + '.txt', mode='w') as txtfile:
    TypeError: can only concatenate list (not "str") to list

代码将正确的文本文件名返回给变量赋值。我的脚本中存在范围界定问题,在更正后解决了问题。