Python搞乱我的Windows文件夹图标

时间:2013-06-14 08:29:11

标签: python file-io python-2.7

我正在使用图标颜色代码来组织某些文件夹中的数据。在视觉上,您可以立即知道该文件夹包含哪种类型的数据。

这些文件夹由软件提供。到目前为止,我手动整理数据(每天花费我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]))

有什么想法吗? 感谢

1 个答案:

答案 0 :(得分:1)

这可能是由许多事情引起的,但我的猜测是,当您从文件夹中复制所有文件时,您还要复制包含桌面配置的Desktop.ini文件。

这样,您已经覆盖了以前的文件夹配置(包括文件夹的图标)

尝试做类似的事情:

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)) and 'Desktop.ini' not in f ]
    return file_list_without_path

More information about Desktop.ini