根据第一个字符移动相应文件夹中的文件 (Python)
Move files on respective folder based on first char (Python)
我有这个文件夹结构:
在 mixed
文件夹中有各种文件,名称的第一个字符是从 0 到 9 的数字。我想做的是将所有以 0 开头的文件移动到 0文件夹,都是以1对1开头的文件夹等等..
我尝试了 解决方案,但它似乎不起作用。
您可以使用 os
模块。
import os
path = <path_of_main_folder>
for file in os.listdir(path + '\mixed'):
full_path = path + '\mixed\' + file
os.rename(full_path, path + '\' + file[0] + '\' + file) #move from mixed to folder w/ same first char
我有这个文件夹结构:
在 mixed
文件夹中有各种文件,名称的第一个字符是从 0 到 9 的数字。我想做的是将所有以 0 开头的文件移动到 0文件夹,都是以1对1开头的文件夹等等..
我尝试了
您可以使用 os
模块。
import os
path = <path_of_main_folder>
for file in os.listdir(path + '\mixed'):
full_path = path + '\mixed\' + file
os.rename(full_path, path + '\' + file[0] + '\' + file) #move from mixed to folder w/ same first char