我有一个Python脚本,该脚本根据特定的通配符模式和特征来打印文件名。然后,我将几个命令输送到此打印出列表,然后删除列出的文件。该脚本是在UNIX服务器上运行的,通常,我将运行脚本如下:

问题描述 投票:0回答:3
python deletefiles.py <directory> <wildcard1> | tee deletedfiles.txt | xargs rm

tee
命令已经在脚本中,以便用户不必将其输入命令中。

我已经在网上看,人们对

xargs rm

说,但我不了解如何与脚本相连的全部语法。 

现在,在UNIX系统上运行的代码只是

import subprocess
我要做的是实现另一个参数的检查,分析/删除,其中
if len(sys.argv) == 3: try: input_directory = sys.argv[1] input_delete = sys.argv[2].split(',') \\patterns to delete except ValueError: "Please enter a valid input"

让脚本可以执行打印文件名的工作,如果参数为

analyze

,则脚本将
delete

和这些文件。 我不确定如何甚至对此进行处理,因为用我的python脚本打印文件的很大要点是,在删除/保存到文本文件时,

xargs rm

tee
有一些要解决的问题。我想知道是否有一种有条件地在python脚本中进行此操作的方法

如果我需要提供有关脚本的更多信息,我很乐意履行义务
    
您可以将它们作为子过程运行。但是,有更好的选择。参见下面

填充
xargs rm
包含您的文件名,
tee deletedfiles.txt

nove,这两个操作都在本机中进行的工作都是微不足道的。

files

我认为您会同意后者更简单,更优雅(尽管在Python 3中,也可以简化得多;您真的想准备尽快切换到Python 3。)
    

我刚刚实施了类似的解决方案:

from subprocess import Popen, PIPE p = subprocess.Popen("tee '{0}' | xargs rm".format(teefile), shell=True, stdin=PIPE) p.communicate('\n'.join(files))

python python-2.7 unix
3个回答
0
投票

我最初只是要发表评论,但我认为这更容易理解。

Python有一个称为

argparse

的模块来处理命令行参数。这些示例有点令人困惑,所以我建议您只是盯着下面的
with open(teefile, "w") as tee: for file in files: tee.write(file + '\n') os.unlink(file)

直到有意义。
试图将其输送到
subprocess

import sys stdin_str: str = None has_stdin: bool = False if not sys.stdin.isatty(): stdin_str = "".join(sys.stdin)[:-1] has_stdin = True sys.stdin = open("/dev/tty", "r")
,您可以在Python脚本本身中完成所有这些操作。

提供一个可以建立的示例:

get_args

如果您只想保存应删除哪些文件,请将输出重定向到文件

0
投票

tee

如果您想删除它们:

-1
投票

xargs rm

然后您可以#!/usr/bin/env python2 # allows us to use python3 print function from __future__ import print_function import os import fnmatch import sys import argparse def get_args(): parser = argparse.ArgumentParser() # note directory has `required=True`, that means the user must provide it parser.add_argument("--directory", help="Directory to analyze", required=True) # store_true stores a boolean depending on whether or not the flag was present parser.add_argument("--delete", help="Delete the files which should be removed", action="store_true", default=False) args = parser.parse_args() # parses, makes sure all conditions are met, exits if user didn't provide a filename return args def analyze(directory): # do whatever wildcarding here to return a list of files # heres just an example, remove all *.txt files recursively files_to_delete = [] for root, dirnames, filenames in os.walk(directory): # fnmatch is unix-like filename matching for filename in fnmatch.filter(filenames, "*.txt"): # abspath gives us the full path, root is the original directory files_to_delete.append(os.path.abspath(os.path.join(root, filename))) return files_to_delete def delete(files): # write to deleted_files.txt in this function deleted_file = open("deleted_files.txt", 'w') for f in files: if os.path.exists(f): # filepath exists if os.path.isfile(f): # is a file (not a directory) os.remove(f) # remove it deleted_file.write("{}\n".format(f)) # write to the file else: print("Could not remove {}, is not a file".format(f), file=sys.stderr) else: print("Could not remove {}, does not exist".format(f), file=sys.stderr) deleted_file.close() def main(): args = get_args() files = analyze(args.directory) # if the user gave us the --delete flag if args.delete: delete(files) else: # just print it out to the user for f in files: print(f) if __name__ == "__main__": main()

获取已删除的文件。

为我使用的功能提供了一些文档:

python2.7 deletefiles.py --directory test_dir > files_to_delete.txt

python2.7 deletefiles.py --directory test_dir --delete

cat deleted_files.txt

fnmatch.filter

sys.stderr

os.path.join

os.path.abspath

os.path.isfile
如果您想删除目录,请看一下
os.walk

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.