Python 拖放
我在这里开发一个解析器,它打开一个文件,读取它并在另一个文件中打印数据。
输入文件由 sys.argv[1] 确定,以处理命令行打开和拖放(在 Windows 中)。 但是,当拖放文件时,它让我
ioerror 13: Permission denied
查看 sys.argv 包含的内容,我执行了以下操作(从 cmd.exe)以使其包含相同的内容:
C:\>python C:\test\iotest.py C:\test\iotestin.txt
它失败了。 然而,以下作品
C:\>cd test
C:\test>python iotest.py iotestin.txt
对我来说,上面的内容实际上/应该是相同的。
- 为什么我会收到权限错误?
- 如何使 python 能够处理完全指定的路径? (如果这就是问题所在。)
- 如何启用拖放功能?
哦,如果不清楚,我将 input/txt 文件拖到 python 文件中,而不是相反。 作为一名编码员,我总是更喜欢 CLI,但该软件的未来用户并不喜欢,因此我需要让它正常工作。
虽然非常简单,但这里有一些代码可以重现该问题:
import sys
print sys.argv
raw_input("")
try:
print "opening",sys.argv[1]
infile = open(sys.argv[1])
outfile = open("out.txt", "w")
raw_input("")
except IndexError:
print "usage:",sys.argv[0].split("\\")[-1],"FILE"
raw_input("")
exit()
except IOError as (errno, strerror):
print "I/O error({0}): {1}".format(errno, strerror)
raw_input("")
exit()
raw_input("done")
Im working on a parser here that opens a file, reads it and prints data in another file.
The input file is determined from sys.argv[1] to both handle commandline opening and drag and drop (in windows). However, when drag and dropping a file, it gives me
ioerror 13: Permission denied
Looking at what sys.argv contained, I did the following (from cmd.exe) to have it contain the same:
C:\>python C:\test\iotest.py C:\test\iotestin.txt
It failed. However, the following works
C:\>cd test
C:\test>python iotest.py iotestin.txt
To me, the above would/should be virtually the same.
- Why do I get the permission error?
- How do I make python able to handle fully specified paths? (If thats the problem.)
- How do I enable drag and drop?
Oh, and if its unclear, I drag the input/txt file to the python file, not the other way around. As a coder, I always prefer a CLI, but the future users of this software do not, hence I need to get this working.
Although extremely simple, heres some code to reproduce the problem:
import sys
print sys.argv
raw_input("")
try:
print "opening",sys.argv[1]
infile = open(sys.argv[1])
outfile = open("out.txt", "w")
raw_input("")
except IndexError:
print "usage:",sys.argv[0].split("\\")[-1],"FILE"
raw_input("")
exit()
except IOError as (errno, strerror):
print "I/O error({0}): {1}".format(errno, strerror)
raw_input("")
exit()
raw_input("done")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您使用
outfile = open("out.txt", "w")
- 在第一个示例中,这将转到 c:\out.txt,我想它是您的错误。You use
outfile = open("out.txt", "w")
- In the first example, this would go to c:\out.txt, which I'd imagine is the source of your error.当出现错误时,工作目录可能位于
C:\Window\System32
:IOError: [Errno 2] No such file or directory: 或 13: Permission returned。所以你需要先切换到脚本或输入文件目录。 如:
os.chdir(os.path.split(sys.argv[0])[0])
如果要切换到输入文件的文件夹,请尝试:
os.chdir(os.path.split(sys.argv[0])[0]) 。 chdir(os.path.split(sys.argv[1])[0])
The working directory may be in
C:\Window\System32
when get error: IOError: [Errno 2] No such file or directory: or 13: Permission denied.So you need to change to the script or input file directory firstly. Such as:
os.chdir(os.path.split(sys.argv[0])[0])
If you want to change to the folder of input file, try:
os.chdir(os.path.split(sys.argv[1])[0])