我环顾四周,没有发现任何类似的东西,所以
我有几个需要解压缩/移动到特定文件夹(同一文件夹)的文件目录。需要解压缩的文件在此结构中:
-ZippedfolderA
-unzipped filename: abc.txt
-ZippedfolderB
-unzipped filename: abc.txt
-ZippedfolderC
-unzipped filename: abc.txt
etc..
因此,您可以看到,解压后,它们都具有相同的名称,因此会相互覆盖
简要说明:
到目前为止,我的代码解压、比较日期,并将文件移动到需要的位置。但是我需要在每个有日期的文件夹之后暂停代码,以便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)
目前没有回答
相关问题 更多 >
编程相关推荐