0

I'm receiving an IOError when trying to create a file using open() in python, which only seems to occur for a single filename. The directories definitely exist and permissions are granted, the loop created around 1000 files successfully. When epic = "CON" in the code below I receive the "No such file or directory" error, but it works fine for other values.

f = open('data\\LSE\\%s.csv' % epic.strip(),'w')
f.write(u.read())
f.close()

Could this be a race issue? The files are created quite quickly.

I'm new to python so if there's something obvious I missed, apologies!

7
  • What does print repr(epic) print? Commented Feb 28, 2014 at 22:19
  • 'COD'. There's more weirdness, using the console, open('data\\LSE\\COD',w) gives "Bad File Descriptor" but CAD, CO, CODD all work fine. I also tried testing this in other directories and encounter the same behaviour Commented Feb 28, 2014 at 22:28
  • open('data\\LSE\\COD.csv',w) or open('data\\LSE\\COD',w)? Commented Feb 28, 2014 at 22:30
  • Ah, sorry, COD should be CON, that was the string that I was having issues with Commented Feb 28, 2014 at 22:31
  • Michael, I tried both (see my correction, it was actually CON sorry), without a file extension it gives "Bad File Descriptor" and with it gives "No such file or directory" Commented Feb 28, 2014 at 22:32

1 Answer 1

2

The problem is that you are running this code on Windows, which still contains some legacies from MS-DOS 1.0. CON is a special name for the console device. You can't use it as a file name. The earliest versions of MS-DOS did not support directories, nor did they support the so-called "extension" of the 8.3 file naming pattern. As a result, the name is special regardless of the directory and regardless of extension.

Some references:


http://msdn.microsoft.com/en-us/library/windows/desktop/aa365247%28v=vs.85%29.aspx

Do not use the following reserved names for the name of a file:

CON, PRN, AUX, NUL, COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9, LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, and LPT9. Also avoid these names followed immediately by an extension; for example, NUL.txt is not recommended.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks dsh, I suppose I'll have to change my stock ticker file naming convention!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.