Python 使用readtxt文件创建目录
我正在尝试使用读取文件创建目录,但它不起作用Python 使用readtxt文件创建目录,python,for-loop,directory,readfile,txt,Python,For Loop,Directory,Readfile,Txt,我正在尝试使用读取文件创建目录,但它不起作用 x= open(r'C:\Users\Fast Computer\Desktop\k.txt', 'r') for f in x: path=r'C:\Users\Fast Computer\Desktop' n=f.readline() path=os.path.join(path,n) os.mkdir(path) 我不知道你的文件包含什么,但是 在你的例子中 第2行已在读取文件中的行 第4行将失败,因为您试图从
x= open(r'C:\Users\Fast Computer\Desktop\k.txt', 'r')
for f in x:
path=r'C:\Users\Fast Computer\Desktop'
n=f.readline()
path=os.path.join(path,n)
os.mkdir(path)
我不知道你的文件包含什么,但是 在你的例子中 第2行已在读取文件中的行 第4行将失败,因为您试图从字符串运行readline()命令 同时,从文件中读取的行包含换行符,因此应该将它们剥离 例如:
x= open(r'C:\Users\Fast Computer\Desktop\k.txt', 'r')
for f in x:
path=r'C:\Users\Fast Computer\Desktop'
n=f.strip()
path=os.path.join(path,n)
os.mkdir(path)
你能提供文本文件的内容吗?
os.mkdir()
如果路径需要递归目录创建,则失败。
改用os.makedirs()