當我第一次將它編碼爲單個函數時,它工作正常。但是當我檢查目錄中的文件時,我想要做更多的事情,所以我將代碼分成了兩個函數:一個檢查以* .rar擴展名結尾的目錄上的文件,如果它找到匹配的文件,它解壓到一個目錄。將'文件'變量傳遞給函數時的TypeError
import shutil, os, patoolib, fnmatch, glob
def unrar():
patoolib.extract_archive(file, outdir="/root/tree/def")
def chktree():
for file in glob.glob('/root/tree/down/*'):
if fnmatch.fnmatch(file, '*.rar'):
unrar()
chktree()
的if
後執行unrar()
從功能chktree():
不起作用。我想知道我在做什麼錯了,這是輸出:
Traceback (most recent call last):
File "autotube.py", line 16, in <module>
chktree()
File "autotube.py", line 14, in chktree
unrar()
File "autotube.py", line 6, in unrar
patoolib.extract_archive(file, outdir="/root/tree/def")
File "/usr/local/lib/python2.7/dist-packages/patoolib/__init__.py", line 676, in extract_archive
util.check_existing_filename(archive)
File "/usr/local/lib/python2.7/dist-packages/patoolib/util.py", line 389, in check_existing_filename
if not os.path.exists(filename):
File "/usr/lib/python2.7/genericpath.py", line 26, in exists
os.stat(path)
TypeError: coercing to Unicode: need string or buffer, type found
是否有任何理由,你不只是'glob.glob('/ root/tree/down/*。rar')'? – mgilson
不要將'file'設置爲變量名,它在Python中的built_in名稱 – Arman
[是否使用內置函數名稱作爲屬性或方法標識符是不好的做法?](http://stackoverflow.com/questions/9109333/is-it-it-bad-practice-to-use-a-built-in-function-name-as-an-a-attribute-or-method-ide) –