Reputation: 52371
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.
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")
Upvotes: 0
Views: 4023
Reputation: 139
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])
Upvotes: 0
Reputation: 321766
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.
Upvotes: 4