[英]How to move a bunch of textfiles into its corresponding folders using python?
我基於列表[1,2,3,....,i]創建了幾個txt文件,分別是mesh1,mesh2,....,mesh(i),我想創建相應的文件夾folder1,folder2 ,....,folder(i)並將txt文件放入其文件夾中。 例如,mesh1到folder1,mesh(i)到文件夾(i)。
for i in lst:
os.makedirs("/home/tianxiangwang/Desktop/Simulation/File{}".format(i)) #This is to create multiple folders
下一步應使用哪些命令將文件移動到文件夾?
我試過了,但顯然不起作用。
os.rename("/home/tianxiangwang/Desktop/Simulation/Mesh{}.txt".format(i), "/home/tianxiangwang/Desktop/Simulation/File{}/Mesh{}.txt".format(i))
以下代碼對我有用。
for item in items:
os.makedirs("/home/tianxiangwang/Desktop/Simulation/"+str(item))
fil="/home/tianxiangwang/Desktop/Simulation/"+str(item)+"/"+str(item)+".txt"
os.rename("/home/tianxiangwang/Desktop/Simulation/"+str(item)+".txt",fil)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.