【发布时间】:2014-11-11 21:10:42
【问题描述】:
我正在尝试制作一个脚本,将您桌面中的所有 .txt 文件移动到 desktop/org,代码如下:
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: 'module' object has no attribute 'copy'。但是,如果我删除最后一行“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
【问题讨论】:
-
你能发布错误的完整堆栈跟踪吗?
-
这段代码在我的 Mac 上的 Python 2.7 下运行良好。我想你从你的其他程序中调用
main(),因为没有.move()。 -
这对我来说在 CentOS 中工作得很好,只要你有现有的目录!
-
对不起,我想说.main()。我已经在帖子上编辑了。我还添加了回溯。我正在使用 python 3.2 顺便说一句
-
目录确实存在。如果我将此脚本作为模块导入并从命令行调用它的 .main() ,它对我来说效果很好。问题是我不能让它作为一个自动执行的脚本工作。
标签: python