I have an annoying problem in python 2.7 on windows XP. I’ve got some code that collects a file name off the command line with the argparse library. I then try and open said file. Normally, this works fine, and if you pass in a full path name it successfully opens that too. However, if the path uses a drive letter other than the location you started from, python fails with an IO error, stating that the file or directory does not exist.
For example:
C:\>schema_split.py "C:\path\to\file"
works!
C:\>schema_split.py "I:\path\to\file"
fails!
Relevant code section:
parser = argparse.ArgumentParser(description='Process the Accounting file.', version='%(prog)s 1.1')
parser.add_argument('infile', nargs="+", type=str, help='list of input files')
# get the current arguments and put them into a variable
args = parser.parse_args()
for f in args.infile:
with open(f, "rb") as mycsv:
I don’t know why python would have problems with alternate drive letters. The only thing I can come up with is that we run it on a shared drive mapped to a local drive. But for all intents and purposes, the program shouldn’t “see” the fact that it is operating on a remote drive.
Thoughts?
You are assuming python is having problems with drive letters. It isn’t. Your problem is something else.
As you can see opened a file from another drive using backslashes without error.
Use the following script to diagnose your problem:
Pass your path to the script and it will test the path and file name you pass to it.