我环顾四周,并没有遇到任何类似的事情......
我有几个文件目录需要解压缩/移动到特定文件夹(同一文件夹)。需要解压缩的文件采用以下结构:
-ZippedfolderA
-unzipped filename: abc.txt
-ZippedfolderB
-unzipped filename: abc.txt
-ZippedfolderC
-unzipped filename: abc.txt
etc..
所以你看到他们解压后都有相同的名字,因此互相覆盖。
快速说明:
到目前为止,我的代码解压缩,比较日期,并将文件移动到需要的位置。但我需要以某种方式暂停每个日期文件夹后的代码,以便数据库可以在解压缩之间拉入文件。否则最后一次解压缩会覆盖其他两个。我试图找出如何/在哪里可以使用像输入或 Break 这样的东西等到第一组被拉入,然后继续解压缩第二个文件夹,等待,然后拉入其他文件夹,依此类推。这是我的代码到目前为止......
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)