2017-08-14 66 views
-1

我有一個小腳本我正在處理「碎化」單個文件(零文件的字節),我試圖使它能夠根據是否通過命令行接收到-file或-directory標誌/ arg來將目錄中的每個文件都碎片化。 這對單個文件工作正常,但我得到一個:如何執行一個目錄中的所有文件或基於args的單個文件的功能,在python

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") 
+0

什麼是'args'。 ['argparse.ArgumentParser().parse_args()']的返回值(https://docs.python.org/3.6/library/argparse.html#argparse.ArgumentParser)? –

+0

「bar」在哪裏定義? – Kevin

+0

你可以發佈你得到的_full_追蹤? –

回答

1

os.listdir返回目錄中的文件名

for f in os.listdir(srcpath): 
    full_path = os.path.join(f, srcpath) 
    ... 
    shredfile(full_path, zeros) 
    ... 
    print("Completed") 
+0

感謝這工作完美,一旦實施,立即解決問題 – mypython

0

我猜你不能一絲一毫的目錄,但只有個別的文件。

shredfile(os.path.join(srcpath, d), zeros) 
+0

謝謝你的幫助,這個答案是正確的以及上面的問題,現在問題已經修復。非常感謝你的幫助@dnozay – mypython

0

這將是這樣做有道。您只需添加進度條和參數解析等部件即可。 您還需要添加一些try-except塊來控制OSErrors發生的情況,如權限被拒絕和類似的東西。



import sys 
import os 

def shredfile (source, filebytes=16*"\0"): 
    f = open(source, "rb") 
    f.seek(0, 2) 
    size = f.tell() 
    f.close() 
    if len(filebytes)>size: 
     chunk = filebytes[:size] 
    else: 
     chunk = filebytes 
    f = open(source, "wb") 
    l = len(chunk) 
    n = 0 
    while n+l<size: 
     f.write(chunk) 
     n += l 
    # Ensure that file is overwritten to the end if size/len(filebytes)*len(filebytes) is not equal to size 
    chunk = filebytes[:size-f.tell()] 
    if chunk: f.write(chunk) 
    f.close() 

def shreddir (source, filebytes=16*"\0", recurse=0): 
    for x in os.listdir(source): 
     path = os.path.join(source, x) 
     if os.path.isfile(path): 
      shredfile(path, filebytes) 
      continue 
     if recurse: 
      shreddir(path, filebytes, 1) 

def shred (source, filebytes=16*"\0", recurse=0): 
    if os.path.isdir(source): 
     shreddir(source, filebytes, recurse) 
     return 
    shredfile(source, filebytes) 

if len(sys.argv)>1: 
    print "Are you sure you want to shred '%s'?" % sys.argv[-1], 
    c = raw_input("(Yes/No?) ").lower() 
    if c=="yes": 
     shred(sys.argv[-1]) 
# Here you can iterate shred as many times as you want. 
# Also you may choose to recurse into subdirectories which would be what you want if you need whole directory shredded 

相關問題