我有一个小脚本,我正在工作的“碎片”单个文件(零字节的文件),我正试图使它,我可以碎片的基础上,如果它收到一个文件或目录标志/arg通过命令行目录中的每个文件。 它对单个文件工作正常,但我得到一个:
IsADirectoryError: [Errno 21] 'Path/To/Directory' error when trying to use it on all files in a directory
。我已经尝试了一些方法来安排循环,使他们的工作,但没有到目前为止。我希望这里有人能帮我纠正这个问题。你知道吗
我有argparser
设置,这样-F file, -D directory, -s srcpath, -i iterations
和bar有关的任何内容都只是我的进度条,它显示脚本在运行时进入进程的程度。你知道吗
这是我的密码:
parser= argparse.ArgumentParser()
parser.add_argument('-F', '--file', action=store_true)
parser.add_argument('-D', '--directory', action=store_true)
parser.add_argument('-s', '--source', required=True, type=str)
parser.add_argument('-i', '--iterations', required=True, type=int)
args = parser.parse_args()
bar = Bar ("Progress: ", max=args.iterations)
srcpath = "path/to/file/"
def shredfile(source, filebytes):
with open(source, 'r+b') as f:
byte = f.read(1)
while byte:
f.write(filebytes)
byte = f.read(1)
zeros = ("0000000").encode('UTF-8')
if args.file:
x=0
for x in range (args.iterations):
shredfile(srcpath, zeros)
bar.next()
bar.finish()
print("Completed")
if args.directory:
for d in os.listdir(srcpath):
x=0
for x in range (args.iterations):
shredfile(srcpath, zeros)
bar.next()
bar.finish()
print("Completed")
我猜你不能分解一个目录,而只能分解单个文件。你知道吗
这是一个正确的方法。您只需添加进度条和参数解析等部分。 您还需要添加一些try-except块来控制操作错误时发生的情况,比如权限被拒绝和类似的事情。你知道吗
os.listdir
返回目录中的文件名相关问题 更多 >
编程相关推荐