Python 文件操作

文件移动

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)
            i = 1
            file1 = file
            while os.path.exists(os.path.join(move_path,file1))
                file1 = ('_d'+str(i).rjust(3,'0')).join(os.path.splitext(file))
                i += 1
            #或者如果存在先删掉
            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文件/目录操作方法

posted on 2020-02-26 17:39  iUpoint  阅读(189)  评论(0编辑  收藏  举报

导航