Python 文件操作

laohyx 2020-02-26

文件移动

import os
# from main import run
#import shutil
path_to_watch = r‘C:\Users\Administrator\Desktop\test\path_to_watch‘
# file_path = r"C:\Users\Administrator\Desktop\test\tmp"
#right_path = r"C:\Users\Administrator\Desktop\test\right_path"
#wrong_path = r"C:\Users\Administrator\Desktop\test\wrong_path"
move_path = r"C:\Users\Administrator\Desktop\test\move_path"
while 1:
    if os.listdir(path_to_watch):
        print("ppp", os.listdir(path_to_watch))
        for file in os.listdir(path_to_watch):
    #         # run(path_to_watch, right_path, wrong_path)
    #         print("file", file)
            file1 = ‘1‘.join(os.path.splitext(file)) if os.path.exists(os.path.join(move_path,file)) else file
            #或者如果存在先删掉
            os.rename(os.path.join(path_to_watch,file), os.path.join(move_path,file1))
            #os.remove(os.path.join(path_to_watch, file))

参考资料:

os文件/目录操作方法

相关推荐