我是Python新手,我正在尝试编写一些东西,可以获取位于不同目录中的文件并解压缩它们(*.Z文件)。我正在尝试理解在不重复下面代码的情况下执行此操作的最佳方法。但是,除了为每个文件再次添加同一行之外,是否可以为每个文件名以某种方式循环该行?例如,一个文件可能位于/root/xxx/yyy.Z中,另一个文件位于/root/xxx/zzz.Z和/root/xxx/aaa.Z中
我可以使用指向每个目录的参数来运行这个函数,但这感觉很笨拙,但仍然有效
def UnzipFiles(pathtofile1,pathtofile2,pathtofile3):
for filename in os.listdir(pathtofile1):
if filename.endswith(".Z"):
retcode = subprocess.call(['gunzip', pathtofile1])
else:
if retcode != 0:
raise IOError('unzipfiles exited with code %d' % retcode)
for filename in os.listdir(pathtofile2):
if filename.endswith(".Z"):
retcode = subprocess.call(['gunzip', pathtofile1])
else:
if retcode != 0:
raise IOError('unzipfiles exited with code %d' % retcode)
for filename in os.listdir(pathtofile3):
if filename.endswith(".Z"):
retcode = subprocess.call(['gunzip', pathtofile3])
else:
if retcode != 0:
raise IOError('unzipfiles exited with code %d' % retcode)
我必须澄清一些我在最初的帖子中没有提到的事情。(1) .Z文件组是它们自己的目录,例如一组文件位于/root/xxx/yyy.Z中,另一组文件位于/root/yyy/zzz.Z和/root/aaa/aaa.Z中。所以我需要循环浏览三个不同的目录。(2)大约有20个左右的文件正在解压缩。这项活动最多每月进行一次
把它变成一个函数似乎为时过早,因为它的硬编码只能处理3个“文件”参数,即使修改为一个参数,它仍然绑定到
".Z"
扩展名。这使得它基本上只适用于一个超特定的任务:使用特定的扩展名解压确切地3个文件(真正的目录…)此外,目录和文件路径之间似乎存在混淆
os.listdir
拉入目录中的所有文件,这似乎与您命名变量的方式相反,这表明您正在传递文件路径无论哪种方式,您都可以删除函数中的多个块并将根文件夹传递给路径,或者相反,跳过
os.listdir
并为subprocess.call
提供一个直接的单路径我建议内联编写代码:
如果您真的经常这样做,以致对呼叫方来说太混乱,您可以使用:
如果您只想提取3个特定文件,而不是3个特定目录:
由于
gunzip
接受多个参数,您可以利用这一点将整个过程归结为:注意,这使用了Python 3.8中的胡须分配语法
:=
顺便说一句
是一种更复杂的写作方式
我建议:;函数应该是
lower_camel_case
UpperCamelCase
是为类保留的可以在python中使用zip函数同时迭代3个变量
这基本上是一样的
只需更改1个输入文件的代码并选中os walk(),然后就可以使用
map()
传递文件列表相关问题 更多 >
编程相关推荐