问题描述
我正在尝试减少以下代码,并可能创建一个可重用于解压缩文件的功能。目前,它执行以下操作:
- 遍历目录并查找当年和月份(YYMM *)“其中*无关”的文件夹,其中包含特定文件(例如file.tar)
- 测试文件夹和所需的tar文件以查看其是否具有特定的读/写权限,如果是,则生成.txt文件作为日志记录形式,并且不允许重复引用锁定的文件
- 使用未锁定(拒绝读/写)并包含我要查找的特定文件的文件(例如file.tar),解压缩该文件并将内容保留在原始文件夹中
- 解压缩完成后,删除tar文件并将tar文件内容保留在文件夹中
目前,我想找出文件/文件夹是否已锁定的唯一方法是通过硬编码值。
import os,re,tarfile
from datetime import datetime
dateTimeObj = datetime.now()
curr = dateTimeObj.strftime('%y%m.')
path = r'C:/Users/UserName/Documents/TestFolder/Folder/'
Path_to_example_tarfile_parent_list = [] #Defines list for example specific folders
RXList = []
def oswalk_directory(your_path):
for directory_path,subdirectories,files in os.walk(path):
for each_folder_name in subdirectories:
#Add path+folder_name to end of each folder path
for each_folder_name in subdirectories:
Path_to_example_tarfile_parent_list.append(path+each_folder_name)
#print (each_folder_name)
if re.search('example_Logs',each_folder_name) :#Traverse directories specific directories that have example_Logs folder
Path_to_example_tarfile_parent_list.append(path+each_folder_name)
oswalk_directory(path)
#Create a list of directories to traverse in current year and month:
print(os.getcwd())
print (Path_to_example_tarfile_parent_list)
for i in range(len(Path_to_example_tarfile_parent_list)):
#If a directory/folder has write permissions
if(os.stat(Path_to_example_tarfile_parent_list[i]).st_mode == 16895):
print("Checking file permissions RW = ok")
for directory_path,files in os.walk(Path_to_example_tarfile_parent_list[i]):
for each_folder_name in subdirectories:
print ("Just before checking for example_Logs")
if re.search('example_Logs',each_folder_name) :#Traverse directories specific directories that have example_Logs folder
isFile = False
print("If is not a file check")
print("Print path to file")
print (os.path.abspath(each_folder_name))
print(each_folder_name)
RXList.append((directory_path+'/'+each_folder_name).replace("\\","/")) #Append new list of folders to traverse,replace double slashes with single
isFile = os.path.isfile(directory_path+'/'+each_folder_name+'/example.tar')#Check if file exists in path
if isFile == True:
print("If is a file check")
if(os.stat(directory_path+'/'+each_folder_name+'/example.tar').st_mode == 33206):#Permissions for tar/archive file
#print (tarfile.info(root+'/'+each_folder_name+'/example.tar'))
print("Open tar file")
print(directory_path)
print(directory_path+'/'+each_folder_name+'/')
t = tarfile.open(directory_path+'/'+each_folder_name+'/')
for filename in ['example.tar']:
try:
f = t.extractfile(filename)
except KeyError:
print("Did not find tar filename")
else:
print("Found file")
#tarfile.extract(directory_path+'/'+each_folder_name+'/')
#tarfile.extractfile(directory_path+'/'+each_folder_name+'/') #extract tar file contents to folder
tarfile.close()
print("Close tar file after extraction")
#os.remove(directory_path+'/'+name+'/example.tar')
elif(os.stat(directory_path+'/'+each_folder_name+'/example.tar').st_mode == 33060): #Else if: no write permissions,break
print("Break if file is not writeable")
break
else:#else,there is no example tar file
break
#If a directory has write permissions are denied
if(os.stat(Path_to_example_tarfile_parent_list[i]).st_mode == 16749):
print("If directory has write permissions denied then proceed to opening text file")
found=False#Set found (duplicate indicator) to false prior to loop
#Check to see if No_Write_Permission_Folder exists to store files with denied permissions
isFile = os.path.isfile(path+'tmp/No_Write_Permission_To_SIL_Folder'+curr+'txt')
if isFile == False:
f=open(path+'tmp/No_Write_Permission_To_Folder'+curr+'txt','w+')
f.close
else:
with open(path+'tmp/No_Write_Permission_To_example_Folder'+curr+'txt','r') as Readfile:
for line in Readfile:#For each line in txt file
if re.search(Path_to_example_tarfile_parent_list[i],line): #If current folder matches current line in txt file
found=True #Set found (duplicate) to True,matching line found in txt file
break #terminate from inner loop
if found == False:
with open(path+'tmp/No_Write_Permission_To_example_Folder'+curr+'txt','a') as no_write_file:
no_write_file.seek(0,0) #Set cursor to beginning of file to allow line-by-line printing
no_write_file.write(Path_to_example_tarfile_parent_list[i]+'\n'.replace("\\","/"))
f = open(path+'/start_script.txt','a')
f.close()
解决方法
对于“被拒绝”的文件,请使用set
来存储文件路径而不是文件。您可以在最后写入文件,也可以在收集了一百万个目录之后写入文件,或者执行其他任何操作
my_set={'not_allowed_twice'}
my_set.add('not_allowed_twice')
my_set.add('this_is_fine')
my_set
要获得单个函数,请使用递归函数进行遍历,而不是先遍历然后循环-尽管将逻辑适当地拆分为多个函数(但适合使用多个函数)然后使用此代码可能会更优雅作为具有单个功能界面(如untar_tree()
)的软件包...
这是您的递归遍历器:
import os
def walk_it(folder):
if os.path.isdir(folder):
for f in os.listdir():
if os.path.isfile(f):
#do your logic here
print(f)
elif os.path.isdir(f):
for f2 in os.listdir(f):
print("Keep on walking " + f2)
walk_it(f2)
不用担心对文件权限进行硬编码,它们不会在您的系统上更改。您可以使用stat.filemode(mode)
将权限转换为-rwxrwxrwx
字符串,这将使人们更容易理解正在发生的事情。
这是bash中的解决方案。我已根据您的要求将脚本标记为注释。我已经对该脚本进行了最终测试,并且效果很好。
#!/bin/bash
# 1. Traverse a directory and look in folders that are of the current year and
# month (YYMM*) "where * is don't care" and contain a specific file (example file.tar)
FILENAME="file.tar"
find -type f -iname "${FILENAME}" -newermt "$(date '+%Y-%m-')1" | while read F
do
DIR=$(dirname "${F}")
# 2. Test the folder and desired tar file to see if it has specific read/writing privileges blocked,if so
# generate a .txt file as a form of logging and do not allow duplicate references of locked files
if stat -c %A "$DIR" | grep -q 'drw.rw.rw.' && stat -c %A "$F" | grep -q '.rw.rw.rw.'
then
# 3. With the files that are not locked (read/write denied) and contain the specific file I am
# looking for (example file.tar),untar the file and leave contents in the original folder
tar -xvf "$F" -C "$DIR"
# 4. When untar is complete,remove tar file and leave tar file contents in folder
rm -f "$F"
else
touch "$F"_lock.txt
fi
done
我不确定从阅读您的问题中没有得到两件事:
- 您要筛选哪些所有者/组/公共权限?我假设
-rw-rw-rw-
- tar压缩文件是压缩为tar.gz还是压缩为tar文件?如果它们可以采用
.gz
格式,则必须更改tar -xvf "$F" -C "$DIR"
行以包含-z
标志