We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,524 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

"No such file or directory"

I am going through a tutorial and have come to opening a file. I'm running it
in Command Prompt, but I know there is no problem with that because I have been running
code in it without problems.

Here is the code I'm supposed to be typing:

f = open('newfile.txt','r+')

Yep, one line! But I'm getting this error: "No such file or directory". I do not have a
file there, but I thought you created a file with the same code?
What am I doing wrong?

- WolfShield

3
Contributors
3
Replies
9 Hours
Discussion Span
1 Year Ago
Last Updated
6
Views
Question
Answered
WolfShield
Posting Whiz in Training
258 posts since Oct 2010
Reputation Points: 28
Solved Threads: 6
Skill Endorsements: 0

Reading does not create file, only writing does:

>>> f = open('newfile.txt','w+')
>>> import os
>>> print([ n for n in os.listdir('.') if n.startswith('new')])
['newfile.txt', 'new_top95_s.txt']
>>>
pyTony
pyMod
Moderator
6,314 posts since Apr 2010
Reputation Points: 879
Solved Threads: 987
Skill Endorsements: 26

You have to run kode in same folder as 'newfile.txt' or you have to give correct path.

f = open('c:/somefolder/newfile.txt','r+')

I do not have a
file there, but I thought you created a file with the same code?

Or as tony mention read(r) dos not create a new file only write(w) dos that.

snippsat
Posting Shark
957 posts since Aug 2008
Reputation Points: 482
Solved Threads: 344
Skill Endorsements: 8

Awesome,
Thank you guys so much!

- WolfShield

WolfShield
Posting Whiz in Training
258 posts since Oct 2010
Reputation Points: 28
Solved Threads: 6
Skill Endorsements: 0
Question Answered as of 1 Year Ago by snippsat and pyTony

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.6086 seconds using 2.65MB