简体   繁体   English

Python更改文件路径名

[英]Python Changing File path name

I want to replace my folder path by a string,I am getting an error.我想用一个字符串替换我的文件夹路径,我收到一个错误。
I tried this :我试过这个:

a="ram"
my_list.to_csv(r'E:\'+str(a)+'\4\mani.csv' )

You made string concatenation mistake.你犯了字符串连接错误。 try str.format to avoid such mistakes.尝试str.format以避免此类错误。

import os

a = "ram"
file_path = r'E:\{a}\4\mani.csv'.format(a=a)
directory = os.path.dirname(file_path)
os.makedirs(path, exist_ok=True)
my_list.to_csv(file_path)
>>> a = "ram"
>>> filename = 'mani.csv'
>>> absolute_path = os.path.join('E:', '4', a, filename)
>>> my_list.to_csv(absolute_path)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM