我試圖使該移動所有的.txt文件在您的桌面臺式機/組織的腳本,代碼如下:AttributeError的:「模塊」對象有沒有屬性「[X]」
import os
import shutil
userhome = os.path.expanduser('~')
src = userhome + '/Desktop/'
dst = src+ 'org/'
def main():
txtlist = os.listdir(src)
for file in txtlist:
sortFiles(file)
def sortFiles(file):
if file.endswith(".txt"):
shutil.move(src+file,dst)
main()
如果我執行.py我得到這個錯誤:AttributeError:'模塊'對象沒有屬性'複製'。但是,如果我刪除最後一行「main()」,並將該腳本作爲模塊導入到python命令行中,並從那裏調用.main(),那麼它工作得很好。我怎樣才能使這項工作作爲腳本?
Traceback (most recent call last):
File "C:\Python32\org.py", line 3, in <module>
import shutil
File "C:\Python32\lib\shutil.py", line 14, in <module>
import tarfile
File "C:\Python32\lib\tarfile.py", line 50, in <module>
import copy
File "C:\Python32\lib\copy.py", line 61, in <module>
from org.python.core import PyStringMap
File "C:\Python32\org.py", line 19, in <module>
main()
File "C:\Python32\org.py", line 12, in main
sortFiles(file)
File "C:\Python32\org.py", line 16, in sortFiles
shutil.move(src+file,dst)
AttributeError: 'module' object has no attribute 'move'
我使用Python 3.2
你可以發佈錯誤的完整堆棧跟蹤嗎? – poke
這段代碼在Python 2.7下的Mac上正常工作。我假設你從其他程序中調用'main()',因爲沒有'.move()'。 – holdenweb
這在CentOS中適用於我,只要你有目錄! –