我正在使用图标颜色代码来组织某些文件夹中的数据。从视觉上看,您可以立即知道文件夹包含哪种类型的数据。
这些文件夹由软件提供。到目前为止,我一直在手动对数据进行排序(每天花费 1 小时)。
然后我制作了一个脚本来读取文件名,将它们放在它们所属的位置并删除那些不需要的。
但是,每次我的 Python 脚本访问其中一个文件夹时,它都会将其图标恢复为常规图标(Windows 7 默认文件夹图标)
这是移动文件的代码:
def move_safely_to(destination, file_name_with_path):
if not os.path.exists(destination):
os.makedirs(destination)
try:
shutil.move(file_name_with_path, destination)
except shutil.Error:
i = 0
file_name_without_path = os.path.split(file_name_with_path)[1]
while os.path.isfile(join(destination, file_name_without_path+str(i))):
i += 1
os.rename(file_name_with_path, join(os.path.split(file_name_with_path)[0], os.path.split(file_name_with_path)[1]+str(i)))
shutil.move(file_name_with_path+str(i), destination)
def get_all_files_from(my_path):
"""return all filename in a folder, without their path"""
file_list_without_path = [ f for f in listdir(my_path) if isfile(join(my_path,f)) ]
return file_list_without_path
def get_all_folders_from(my_path):
""" return all folder in a folder without their path"""
folder_list_without_path = [ f for f in listdir(my_path) if not isfile(join(my_path,f)) ]
return folder_list_without_path
def get_all_files_from_folder_and_sub_folders(my_path):
"""return all file in my_path root and sub-directories
Not just file names but their path too.
"""
#getting file from this dorectory
file_list = get_all_files_from(my_path)
sub_folder_list = get_all_folders_from(my_path)
for i, _file_name in enumerate(file_list):
file_list[i] = join(my_path,file_list[i])
for folder_name in sub_folder_list:
return_list = get_all_files_from_folder_and_sub_folders(join(my_path,folder_name))
file_list.extend(return_list)
return file_list
和
os.rename(file_name_with_path, join(file_name_splitted[0], tag+file_name_splitted[1]))
有任何想法吗?谢谢