简体   繁体   中英

Python messing up my windows folder icon

I'm using an icon color code to organise data in some folders. Visually, you instantly know which type of data the folder is containing.

Those folders are being fed by a software. Up until now I was sorting the data manually (which took me 1h per day).

Then I made a script which reads the filenames, put them where they belong and remove those which are not needed.

However each time my Python script accesses one of those folder it revert its icon to the regular one (Windows 7 default folder icon)

This is the code that moves the files:

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

and

os.rename(file_name_with_path, join(file_name_splitted[0], tag+file_name_splitted[1]))

Any ideas? Thanks

This can be caused by many things, but my guess is that when you copy all files from a folder, you also copy the Desktop.ini file that contains desktop configuration.

That way, you already overwrite the previous folder configuration (which includes the folder's icon)

Try doing something like that:

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM