- 论坛徽章:
- 0
|
回复 7# cfwyy
大神,总是提示缩进错误, shutil.copy用缩进吗 ?
>>> import os
>>> import shutil
>>> ewDir="./tmp"
>>> print (ewDir)
./tmp
>>> pyfiles=[f for f in os.listdir('/root') if f.endswith("py") and os.path.isfile(f)]
>>> print(pyfiles)
['loop.py', 'create_dir_debug.py', 'python_cp.py', 'create_dir.py', 'assert.py', 'usb_v2.py', 'os.py', 'input.py', 'raw_input.py', 'readlines.py', 'get_logs_for_date.py']
>>> for file in pyfiles:
... shutil.copy(file,newDir)
File "<stdin>", line 2
shutil.copy(file,newDir)
^
IndentationError: expected an indented block
另外我把上述代码写入了脚本,脚本没有报任何错误,但就是copy结果失败,
cat python_cp.py:
import os
import shutil
newDir="./tmp"
pyfiles=[f for f in os.listdir('/root') if f.endswith("py") and os.path.isfile(f)]
for file in pyfiles:
shutil.copy(file,newDir)
运行结果:
[root@localhost ~]# python python_cp.py
[root@localhost ~]#
确认没copy过去
[root@localhost tmp]# ll *py*
ls: cannot access *py*: No such file or directory
[root@localhost tmp]# pwd
/tmp
|
|