将文件排序到文件夹中的 Python 代码
Python 2.5.1
http://www.cgsecurity.org/wiki/After_Using_PhotoRec
我已经只需运行 PhotoRec,作为将文件类型排序到其自己的文件夹中的方法给出的代码就会返回此错误。 关于如何改变有什么建议吗? 谢谢:
[编辑2:两点:
- 这个问题被否决了,因为它是代码的“使用”,而不是编程问题。 它符合编码问题吗? 我认为是的。
我已经返回并编辑了代码所在的页面,以澄清对参数的需求,以便其他人受益。]
gyaresu$ python recovery.py 回溯(最近一次调用最后一次): 文件“recovery.py”,第 8 行,位于 来源 = sys.argv[1] IndexError: list index out of range
脚本:
#!/usr/bin/env python
import os
import os.path
import shutil
import string
import sys
source = sys.argv[1]
destination = sys.argv[2]
while os.path.exists(source) != True:
source = raw_input('Enter a valid source directory\n')
while os.path.exists(destination) != True:
destination = raw_input('Enter a valid destination directory\n')
for root, dirs, files in os.walk(source, topdown=False):
for file in files:
extension = string.upper(os.path.splitext(file)[1][1:])
destinationPath = os.path.join(destination,extension)
if os.path.exists(destinationPath) != True:
os.mkdir(destinationPath)
if os.path.exists(os.path.join(destinationPath,file)):
print 'WARNING: this file was not copied :' + os.path.join(root,file)
else:
shutil.copy2(os.path.join(root,file), destinationPath)
Python 2.5.1
http://www.cgsecurity.org/wiki/After_Using_PhotoRec
I've just run PhotoRec and the code given as a way to sort file types into their own folder is coming back with this error. Any suggestions on how to alter? Thanks :
[EDIT2: Two points:
- This question was voted down because it was a 'usage' of code, somehow not a programming question. Does it qualify as a coding question? I argue yes.
I've gone back and edited the page where the code came from to clarify the need for parameters for the benefit of others.]
gyaresu$ python recovery.py
Traceback (most recent call last):
File "recovery.py", line 8, in
source = sys.argv[1]
IndexError: list index out of range
Script:
#!/usr/bin/env python
import os
import os.path
import shutil
import string
import sys
source = sys.argv[1]
destination = sys.argv[2]
while os.path.exists(source) != True:
source = raw_input('Enter a valid source directory\n')
while os.path.exists(destination) != True:
destination = raw_input('Enter a valid destination directory\n')
for root, dirs, files in os.walk(source, topdown=False):
for file in files:
extension = string.upper(os.path.splitext(file)[1][1:])
destinationPath = os.path.join(destination,extension)
if os.path.exists(destinationPath) != True:
os.mkdir(destinationPath)
if os.path.exists(os.path.join(destinationPath,file)):
print 'WARNING: this file was not copied :' + os.path.join(root,file)
else:
shutil.copy2(os.path.join(root,file), destinationPath)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它只是意味着程序需要两个命令行参数:源和目标。 如果您希望在另一个函数中使用相同的代码,请将 sys.argv[1] 和 [2] 替换为您自己的变量。
It simply means that the program is expecting two command line arguments: source and destination. If you wish to use the same code in another function, replace sys.argv[1] and [2] with your own variables.
或者您可以修改原始脚本并
在导入语句之后添加以进行正确的错误处理。
Or you can modify the original script and add
after the import statements for proper error handling.
由于脚本将询问路径(如果路径不存在),因此您可以将程序参数设置为可选。
改成
Since the script is going to ask for paths if they don't exist, you could make the program arguments optional.
Change
to