我试图将一些数据写入文件,但我使用的路径有一些问题
这是我的代码:
my_path = r'c:\data\XYM\Desktop\MyFolder 7-sep'
with open(my_path + '\\' + 'Vehicles_MM' + '\\' + name_vehicile + '-AB.txt', 'w') as output:
writer = csv.writer(output, delimiter = '\t')
writer.writerow(headers)
writer.writerow(data)
for vehicle_loc_list in vehicle_loc_dict.values():
for record_group in group_records(vehicle_loc_list):
writer.writerow(output_record(record_group))
这是我收到的错误:
FileNotFoundError: [Errno 2] No such file or directory: 'c:\\data\\XYM\\Desktop\\MyFolder 7-sep\\Vehicles_MM\\20200907-AB.txt'
您应该使用其中一个内置项来处理路径。要么{}要么{}
根据评论中披露的情况,问题是您正试图写入一个子目录
c:\data\XYM\Desktop\MyFolder 7-sep\Vehicle_MM\
,该子目录不存在,实际上您不想写入修复方法是删除目录分隔符
\\
;可能使用不同的分隔符。比如说,如果确实要写入此子目录,则必须确保它存在,然后才能尝试打开其中的文件
使用
pathlib.Path
时,同样的东西更具可读性相关问题 更多 >
编程相关推荐