Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Creating file from editor
#1
I'm reading Automate boring stuff with Python and the given example of creating a file when it's not already there is this one:
baconFile = open('C:\\Python36\\kodovi\\bacon.txt', 'w')
baconFile.write('Hello world!\n')
baconFile.close()
baconFile = open('C:\\Python36\\kodovi\\bacon.txt', 'a')
baconFile.write('Bacon is not a vegetable.')
baconFile.close()
baconFile = open('C:\\Python36\\kodovi\\bacon.text')
content = baconFile.read()
baconFile.close
print(content)
the book says that "since there isn’t a bacon.txt yet, Python creates one."

But I receive an error
Error:
FileNotFoundError: [Errno 2] No such file of directory: 'C:\\Python36\\kodovi\\bacon.txt'
I'm pretty sure that there is such directory as I used it for some previous code and it worked fine. This means that Python is telling me that files cannot be created this way. Was author wrong ( which is hard for me to believe )?
Reply
#2
(May-19-2018, 09:09 PM)Truman Wrote: I'm pretty sure that there is such directory as I used it for some previous code and it worked fine.
There is problem with your directory path.
If you save code under in a folder there will be a bacon.txt in the folder where you saved your.py
with open('bacon.txt', 'w') as f:
    f.write('hello world')
A little annoying that Al Sweigart use a older way(not using with any many other stuff) to write Python,
but i shall try not to be negative Automate boring stuff is kind of okay.
Reply
#3
(May-19-2018, 09:09 PM)Truman Wrote: the book says that "since there isn’t a bacon.txt yet, Python creates one."
The book is right

(May-19-2018, 09:09 PM)Truman Wrote: But I receive an error
Error:
FileNotFoundError: [Errno 2] No such file of directory: 'C:\\Python36\\kodovi\\bacon.txt'
And the exception occurs in the line number ...?!

(May-19-2018, 09:09 PM)Truman Wrote: I'm pretty sure that there is such directory as I used it for some previous code and it worked fine. This means that Python is telling me that files cannot be created this way. Was author wrong ( which is hard for me to believe )?
Don't be sure - check! I think you will find out that you were wrong
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#4
Hm, although I understand your argument have a problem in applying it to my code.
This is what I wrote:
baconFile = open('C:\\Python36\\kodovi\\bacon.txt', 'w')
with open('bacon.txt', 'w') as f:
    f.write('hello world')
baconFile.close()
with open('bacon.txt', 'w') as f:
    f.read('hello world')
content = baconFile.read()
baconFile.close()
print(content)
and got
Error:
Traceback (most recent call last): File "C:\Python36\kodovi\files1.py", line 6, in <module> f.read('hello world') TypeError: integer argument expected, got 'str'
Reply
#5
Wrong function, maybe?

PS In line 7 your baconFile (I hate camels Wall ) is in the wrong state for reading
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#6
f.read('hello world')
It's just f.read().
The point of using with open() is that the file will be closed automatically.
So trow away all close()
with open('bacon.txt', 'w') as f:
    f.write('hello world')

with open('bacon.txt') as f:
    print(f.read())
Output:
hello world
Reply
#7
Thank you, this finally works
baconFile = open('C:\\Python36\\kodovi\\bacon.txt', 'w')

with open('bacon.txt', 'w') as f:
    f.write('hello world')
 
with open('bacon.txt') as f:
    print(f.read())

with open('bacon.txt', 'w') as f:
	f.write('Bacon is not a vegetable.')
with open('bacon.txt') as f:
	print(f.read())
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help creating shell scrip for python file marciokoko 10 1,394 Sep-16-2023, 09:46 PM
Last Post: snippsat
  Creating csv files from Excel file azizrasul 40 5,729 Nov-03-2022, 08:33 PM
Last Post: azizrasul
  Creating file with images BobSmoss 1 1,398 Jan-08-2022, 08:46 PM
Last Post: snippsat
  Music Notation editor; how to build the editor? direction to go? philipbergwerf 1 1,700 Jan-01-2022, 04:56 PM
Last Post: Larz60+
  Creating csv file from another file pisike 0 1,612 Nov-24-2020, 02:02 PM
Last Post: pisike
  Creating Conda env from requirments.txt file ErnestTBass 7 15,766 Apr-23-2020, 06:57 PM
Last Post: snippsat
  Creating a pdf from file DT2000 4 2,345 Apr-03-2020, 07:16 PM
Last Post: DT2000
  Internal Server Error 500 on creating .py file patrickluethi 0 1,766 Aug-02-2019, 01:57 PM
Last Post: patrickluethi
  python script file not opening in IDLE editor srm 2 4,407 Jun-23-2019, 08:45 AM
Last Post: Larz60+
  Creating Dictionary form LOG /text file DG1234 7 5,497 Feb-13-2019, 08:08 PM
Last Post: DG1234

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020