Creating file from editor - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Creating file from editor (/thread-10416.html) |
Creating file from editor - Truman - May-19-2018 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 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 )?
RE: Creating file from editor - snippsat - May-19-2018 (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. RE: Creating file from editor - volcano63 - May-19-2018 (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 errorAnd 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 RE: Creating file from editor - Truman - May-19-2018 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
RE: Creating file from editor - volcano63 - May-19-2018 Wrong function, maybe? PS In line 7 your baconFile (I hate camels ) is in the wrong state for reading
RE: Creating file from editor - snippsat - May-19-2018 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())
RE: Creating file from editor - Truman - May-19-2018 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()) |