-3

This is my code:

import os
os.chdir("C:\\Users\\satvi_000\\Downloads")

if os.path.exists('new_file.txt')==False:  
    create_file= open("new_file.txt",'w')  #This is just to  create the file 
                                                  #in case it doesn't exist
    create_file.close()
file= open('new_file.txt','r+')

data= file.read()

file.write("blah blah blah ")

I want to create a file( if it doesn't already exist) and write some data to it. I'm doing this as a part of a larger program and tested it separately to see what the problem was and I can't quite figure it out yet. I will be writing to this file again and again in the larger program, and the file will also be modified everytime the program is run. What's going wrong here?

Satvik Gupta
  • 53
  • 1
  • 2
  • 7
  • 3
    You don't need to compare `os.path.exists` to a boolean. It already returns a boolean. You can `if not os.path.exists(...)` instead. – Carles Mitjans Jun 16 '17 at 07:08
  • 1
    What is your problem? – moritzg Jun 16 '17 at 07:11
  • Works for me (if I change the chdir name) – cdarke Jun 16 '17 at 07:12
  • _"What's going wrong here?"_ That's what _we_ want to know. What's the problem with this code? Does it throw an error? Does it update the wrong file? Does it write the wrong data to the file? – Aran-Fey Jun 16 '17 at 07:17
  • It's better not to use `os.chdir` but add the path to your `open` call: `filename = r'C:\Users\satvi_000\Downloads\new_file.txt'; create_file = open(filename, 'w')`. – Matthias Jun 16 '17 at 07:46

4 Answers4

2

May be this can help you

Python Print String To Text File

Try to use with open("new_file.txt", "w") as text_file:

RaTh0D
  • 323
  • 3
  • 19
2

Try closing the file in the end. file.close()

Osaf Malik
  • 90
  • 1
  • 8
1
import os
os.chdir("C:\\Users\\satvi_000\\Downloads")

if os.path.exists('new_file.txt')==False:  
    create_file= open("new_file.txt",'w')  #This is just to  create the file 
                                                  #in case it doesn't exist
    create_file.close()
file= open('new_file.txt','r+')

data= file.read()

file.write("blah blah blah ")
file.close()
Exprator
  • 26,992
  • 6
  • 47
  • 59
0

As you are writing the file again and again, so it will keep modifying the file, erasing earlier data present in the same file. If you want to update the file, open the file with append (a) permission and not with write operation.

And in your code, the moment you open a file with 'w' permission, it will create the file by default if the file does not exist. And you can directly write into the file. You dont need to reopen it, it is already open.

Thanks.

  • Welcome to Stack Overflow. Thank you for your answer. Two things to consider as an improvement. 1) Apart from describing the code changes; it'd be also useful to have a complete (fixed up) code example based on OP's example. 2) No need to add _Thanks._ in your answer. For most users here this is an unwanted distraction; keep your answer as concise as possible. Refer to [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer) for further hints; and take the [tour](https://stackoverflow.com/tour) to learn about how Stack Overflow works. – Ivo Mori Aug 03 '20 at 11:38