Why does my last data on csv get delete by python [duplicate] - python

This question already has answers here:
How do I append to a file?
(13 answers)
Closed 2 years ago.
I'm new to python and I'm working on a little writing in CSV project
but every time that I run it it will delete my last data and write a new one
this is the code
with open('data.csv', 'w', newline='')as f:
User_new_pass = input('enter your new password: ').strip()
User_new_id = ' ' + input('enter your new user name: ').strip()
User_new_info = [User_new_pass, User_new_id]
linewriter = csv.writer(f)
linewriter.writerow(User_new_info)

If you want to append to the file, you should change the mode in the open() function:
with open('data.csv', 'a', newline='')as f:
# your code
a - Open file in append mode. If file does not exist, it creates a new file.
w - This Mode Opens file for writing. If file does not exist, it creates a new file. If file exists it truncates the file.

Related

Python Tex tfile [duplicate]

This question already has answers here:
How do I append to a file?
(13 answers)
Closed 1 year ago.
I want to store the inputs in my text file every time I run it. But when I run the script the old inputs gets deleted! how can I fix this?
sample code:
name = input('Enter name: ')
f = open('test.txt', 'w')
f.write(name)
f.close()
You should open the file in append mode:
f = open('test.txt', 'at')
Notice the at, meaning append text. You have w mode, meaning write text (text is default), which first truncates the file. This is why your old inputs got deleted.
With the 'w' in the open() you create a new empty file everytime.
use open('test.txt', 'a+')
Also I would suggest you use the with statement:
name = input('Enter name: ')
with open('test.txt', 'a+') as file:
file.write(name)
write the additional text in append mode
f = open('test.txt', 'a')
'w' is write mode and so deletes any other data before rewriting any new data

Read and write to a CSV within a function/loop in Python [duplicate]

This question already has answers here:
read and write on same csv file
(4 answers)
Closed 3 years ago.
I'm trying to add the functionality of writing information from one CSV to a new CSV file if certain inputs are met. I'm struggling to get the functions that are executed to also write into the new CSV file. This is my current (very basic) function to print the information that I want to add out;
def function():
with open('my_file.csv', 'r') as csv.file:
csv_reader = csv.reader(csv.file)
next(csv_reader)
for line in csv_reader:
print(line)
Thanks in advance - my python is quite primitive!
This might be helpful with your case
fileInput = open(fileInput.csv, 'rb')
fileOutput = open(fileOutput.csv, 'wb')
reader = csv.reader(fileInput)
writer = csv.writer(fileOutput)
for row in reader:
if row[2] == 'Condition':
writer.writerow( row[0], row[1], 'Somevalue')

Append text to a Text file without replacing it Python [duplicate]

This question already has answers here:
Prepend a line to an existing file in Python
(13 answers)
Closed 3 years ago.
Im new to python and I need some help from you guys.
So this is my Code
Tk().withdraw()
filename = askopenfilename(title='Choose a file', filetypes=[("Text Files", "*.txt")])
f = open(filename)
with open(filename,'r+',encoding="UTF-8") as file:
file.write('test')
file.write('\n')
file_contents = f.read()
This is the Text File without using file.write
Im a big noob in python please help me.
And this is after using file.write
test
ig noob in python please help me.
My Goal is to append the Text to the top of the text file without replacing the contect underneath it.
When you write to a file it always effectively overwrites the bytes inside the file stream. What you might want to do instead, is read the file first, and write the necessary parts, and then write your original contents back:
with open(filename,'r+',encoding="UTF-8") as file:
data = file.read()
file.write('test\n')
file.write(data)
This should be all you need. Remove the f = open(filename) and file_contents = f.read() lines, because you are opening the same file twice.
Just copy the content first and insert it in the beginning, like this:
with open(filename,'r+',encoding="UTF-8") as file:
previous_content = file.read()
file.write('test\n' + previous_content)

write function parameter inside file - Python [duplicate]

This question already has answers here:
How do I append to a file?
(13 answers)
Closed 5 years ago.
I have a function which prints messages. then I want to save this messages into a file. But when I use .write(function parameter) it only write last message inside my file
writing_in_log = True
def print_and_log(message):
if write_in_log is True:
logFile = open("log", "w+")
logFile.write(message)
logFile.close()
I suppose you are not using the 'a' parameter when opening the file:
with open('file.txt', 'a') as file:
file.write('function parameter')
You probably open the file for each writing with open(yourfile, 'w') which will erase any content from the file before you write to it. If you want to append to your file, use open(yourfile, 'a').
In case this is not the error we need more information about what you are doing, i.e. the relevant parts of the code.

Adding to a file instead of overwriting data on multiple function calls [duplicate]

This question already has answers here:
How do I append to a file?
(13 answers)
Closed 7 years ago.
I am trying to make a program that takes a value, writes it to a text file, and then repeats the action up to 5 times. The problem is I can't seem to figure out how to make it so that it writes the new input onto a new line. Instead it just overwrites the old input. How can I make it so that each input goes onto a new line without it being overwritten?
My code:
def main():
outfile = open('scorefiles.txt', 'w') #Makes the .txt fiel.
grade = input("Grade: ") #Gets input.
outfile.write(grade + "\n") #Writes input to txt file.
outfile.close() #Closes file.
for _ in range(5): #Repeats main function 5 times.
main()
You need to open the file once and write many times, or just use the append mode:
outfile = open('scorefiles.txt', 'a')

Categories