0

I have a list in the format:

a=["C/C++","Java","Python"]

I am trying to create a text file for each element in the list using python as follows:

for i in a:
    temp=i+".txt"
    with open(temp,"w") as outfile:
          outfile.write("some value")

However am running into issues for the elements off the form "C/C++" as it is trying to interpret that as a dir.

 Traceback (most recent call last):
   File "<stdin>", line 3, in <module>
 IOError: [Errno 2] No such file or directory: 'C/C++.txt'

How do I overcome this??

Note-if the list element does not contain "/" in it, then the code created files for such elements.

1 Answer 1

4

Well the problem is fairly simple.
A file name can't contain any of the following characters: /:*?<>|

So, just change the name to a valid one like C-C++

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

3 Comments

Besides that, there are many other reserved names (under Windows) like CON or COM2 etc. that are illegal as filenames.
"facepalm"!! yeah i know about con etc, but since i am working in ubuntu assumed it did not apply to linux distros! my bad!
@Moontails Check this out :)

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.