[英]Making directory using read txt file
我正在嘗試使用讀取文件創建目錄,但它不起作用。
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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.