中断或用户输入以单独复制文件?

2024-05-26 22:56:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我环顾四周,没有发现任何类似的东西,所以

我有几个需要解压缩/移动到特定文件夹(同一文件夹)的文件目录。需要解压缩的文件在此结构中:

  -ZippedfolderA
    -unzipped filename: abc.txt
  -ZippedfolderB
    -unzipped filename: abc.txt
  -ZippedfolderC
    -unzipped filename: abc.txt
  etc..

因此,您可以看到,解压后,它们都具有相同的名称,因此会相互覆盖

简要说明:

  • 不,解压后我不能更改名称。这些文件被拉入数据库,需要保留给定的名称
  • 文件夹A、B、C之间的区别在于日期。每个文件夹都有不同的日期。A=昨天,B=前天,C=三天前,以此类推

到目前为止,我的代码解压、比较日期,并将文件移动到需要的位置。但是我需要在每个有日期的文件夹之后暂停代码,以便DB可以在解压之间拉入文件。否则,最后一个被解压的将覆盖其他两个。我正试图找出如何/在哪里使用类似于输入中断的东西来等待,直到第一组被拉入,然后继续解压缩第二个文件夹,等待,然后拉入其他文件夹,依此类推。这是到目前为止我的代码

def files_to_folders(filename):
    if f.endswith(".TXT"):
        ff = f[1:7]
        if ff == yesterday.strftime("%y%m%d"):
           print(ff, f)
           fnew = f[12:] # Change back to [13:]
           os.rename(os.path.join(root,f), os.path.join(root, fnew))
           shutil.copy(os.path.join(root,fnew), wescorpex)
           shutil.copy(os.path.join(root, "DLY"), wescorpex)
        elif ff == daybefore1.strftime("%y%m%d"):
            print(ff, f)
            fnew = f[12:] # Change back to [13:]
            os.rename(os.path.join(root,f), os.path.join(root, fnew))
            shutil.copy(os.path.join(root,fnew), wescorpex)
            shutil.copy(os.path.join(root, "DLY"), wescorpex)
        elif ff == daybefore2.strftime("%y%m%d"):
            print(ff, f)
            fnew = f[11:]# Change back to [13:]
            os.rename(os.path.join(root,f), os.path.join(root, fnew))
            shutil.copy(os.path.join(root,fnew), wescorpex)
            shutil.copy(os.path.join(root, "DLY"), wescorpex)
        elif ff == daybefore3.strftime("%y%m%d"):
            print(ff, f)
            fnew = f[10:]# Change back to [13:]
            os.rename(os.path.join(root,f), os.path.join(root, fnew))
            shutil.copy(os.path.join(root,fnew), wescorpex)
            shutil.copy(os.path.join(root, "DLY"), wescorpex)
    return filename

targets = [(folder, create(folder, destpath)) for folder in destdir]
#Create directories based on ZIP folder
try:
    for dirname, full_path in targets:
        for filename in srcfiles:
            if dirname == filename[0:7]:
                shutil.copy(filename, full_path)

        # Extract all files within folders to new directory root
        for root, dirs, files in os.walk(os.path.join(rootpath, full_path)):
            for files in fnmatch.filter(files,pattern):
                zipfile.ZipFile(os.path.join(root,files)).extractall(root)
                os.remove(os.path.join(root,files))
        #Rename extracted files in new direct root with end of filename
        walkpath = os.path.join(rootpath,full_path)
        for root, dirs, files in os.walk(walkpath):
            for f in files:
                files_to_folders(f)

except(IOError):
    print("there was an error.")
    time.sleep(2)

Tags: topathin文件夹forosrootfiles

热门问题