在监视目录时更改 Python 中的文件名编码

问题描述

我使用看门狗库来跟踪我的 Python 脚本中的文件更改事件。我使用 filename.encode("cp1252").decode("utf-8") 解码我的文件名,稍后重命名。例如 "C:\PACIENTI\ElÄ«za.txt".encode("cp1252").decode("utf-8") === "C:\PACIENTI\Elīza.txt" 我最后使用的是 def whatisthis(filename): 并且如果 isinstance 来检查我从“os.listdir("C:\PACIENTI\")”收集的文件名是否是已编码或未编码,仅在需要时调用重命名函数

主要问题是编码正确地发生在看门狗处理程序类之外。第二个问题是“whatisthis(filename):”根本没有发生。

MAIN ISSUE

import os
import time
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler

# FAILU MONITORĒŠANA UN PĀRSAUKŠANA / FILE MONITORING AND RENAMING
# 1) instalē watchdog ar pip / INSTALLING WATCHDOG WITH PIP
# 2) izveido pareizus failu nosaukumus / MAKING CORRECT FILE NAMES.


class Watcher:
    DIRECTORY_TO_WATCH = "C:\\PACIENTI"

    def __init__(self):
        self.observer = Observer()

    def run(self):
        event_handler = Handler()
        self.observer.schedule(
            event_handler,self.DIRECTORY_TO_WATCH,recursive=True)
        self.observer.start()
        try:
            while True:
                time.sleep(5)
        except:
            self.observer.stop()
            # print "Error"

        self.observer.join()




###
### ĀRPUS WATCHDOG RĀDA PAREIZI // THE WATCHDOG THING THAT DOES ENCODING CORRECTLY
for count,filename in enumerate(os.listdir("C:\\PACIENTI\\")):
                # dst ="Hostel" + str(count) + ".jpg"
                # src = 'C:\\PACIENTI\\' + filename
                #print(ascii(src))

                decoded = filename.encode("cp1252").decode("utf-8")
                dst = 'C:\\PACIENTI\\' + decoded
                print(dst)

filename2 = "C:\\PACIENTI\\Elīza.txt";
decoded2 = filename2.encode("latin1").decode("utf-8")
print(decoded2);




###
### ĀRPUS WATCHDOG RĀDA PAREIZI // THE WATCHDOG THING THAT DOES ENCODING CORRECTLY


class Handler(FileSystemEventHandler):

    @staticmethod
    def on_any_event(event):
        if event.is_directory:
            return None

        elif event.event_type == 'created':
            # Take any action here when a file is first created.
            print("izveidots fails")
            for count,filename in enumerate(os.listdir("C:\\PACIENTI\\")):
                # dst ="Hostel" + str(count) + ".jpg"
                # src = 'C:\\PACIENTI\\' + filename
                #print(ascii(src))

                decoded = filename.encode("cp1252").decode("utf-8")
                dst = 'C:\\PACIENTI\\' + decoded
                print(dst)
                # os.rename(src,dst)
                
                #### whatisthis klase vispār neveic neko
                                #### whatisthis klase vispār neveic neko
                                #### whatisthis klase vispār neveic neko


                ## THE FUNCTION THAT FAILS TO CHECK ENCODING
                def whatisthis(filename):
                    if isinstance(filename,str):
                        # os.rename(src,dst)
                        print("nav unikods")
                    elif isinstance(filename,unicode):
                        os.rename(src,dst)
                        print("unikods")
                    else:
                        print("nezinams string")


                                
                #### whatisthis klase vispār neveic neko
                                #### whatisthis klase vispār neveic neko
                                #### whatisthis klase vispār neveic neko

            # print "Received created event - %s." % event.src_path

        elif event.event_type == 'modified':
            print("modificēts fails")
            # Taken any action here when a file is modified.
            # Take any action here when a file is first created.
            for count,filename in enumerate(os.listdir("C:\\PACIENTI\\")):
                # dst ="Hostel" + str(count) + ".jpg"
                src = 'C:\\PACIENTI\\' + filename
                # print(ascii(src))
                decoded = filename.encode("cp1252").decode("utf-8")
                dst = 'C:\\PACIENTI\\' + decoded
                print(dst)
                # os.rename(src,dst)
                # print(filename);

            # rename() function will
            # rename all the files
                def whatisthis(filename):
                    if isinstance(filename,dst)
                        print("unikods")
                    else:
                        print("nezinams string")
            # print "Received modified event - %s." % event.src_path


if __name__ == '__main__':
    w = Watcher()
    print("Automatiskais failu nosaucējs")
    w.run()

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)