Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
csv file saving problem
#1
import csv


def save_users_csv(new):
    locals()
    users_file = open(r'C:\Users\patri\PycharmProjects\air_traffic_control_system\users.csv', "r+", newline='')
    writer = csv.writer(users_file)
    writer.writerows(new)
    print(new)      # test only
    users_file.close()


def read_user_csv():
    global user
    user_file = open(r'C:\Users\patri\PycharmProjects\air_traffic_control_system\users.csv', "r+", newline='')
    reader = csv.reader(user_file)
    user = list(reader)
    user_file.close()
    return user


read_user_csv()
input_user = input("User: ")
for x in range(len(user)):
    if input_user == user[x][0]:
        target = x
        del user[target]
        print("Now you are deleting user ")
save_users_csv(user)
I am trying to delete a user in the CSV file in python. I have tested many about the variables and the list (user) before and after delete function, everything run properly, the user list has been updated too.
However, the CSV file seems not to be updated. Do I have some mistake? Please help! Appreciate.
Reply
#2
when using r+ (read, write) mode, you need to control the stream position before reading or writing
see, seek, tell and flush: https://docs.python.org/3/library/io.htm...OBase.tell

seek has three modes, there are builtin constants for the values, which are:
SEEK_SET - value 0 Sets stream pointer to beginning of file.
SEEK_CUR - value 1 Sets stream pointer to current position
SEEK_END - value 2 Sets stream pointer to end of file.

tell will return the current stream position (relative to BOF)

flush will flush remaining data in buffer to file this step is needed for premature file close, and is a safe bet if unshure.
To avoid all of this, I would just open in the function save_users_csv as 'w' rather than r+ as you are overwriting the entire file anyway.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Saving the times a script is run to a file or ... 3Pinter 7 1,412 Oct-19-2022, 05:38 PM
Last Post: 3Pinter
  Code Assistance needed in saving the file MithunT 0 821 Oct-09-2022, 03:50 PM
Last Post: MithunT
  Saving the print result in a text file Calli 8 1,811 Sep-25-2022, 06:38 PM
Last Post: snippsat
  Trying to determine attachment file type before saving off.. cubangt 1 2,168 Feb-23-2022, 07:45 PM
Last Post: cubangt
  Showing and saving the output of a python file run through bash Rim 3 2,478 Oct-06-2021, 10:48 AM
Last Post: gerpark
  Problem in saving .xlsm (excel) file using pandas dataframe in python shantanu97 2 4,319 Aug-29-2021, 12:39 PM
Last Post: snippsat
  Need help with saving output into an excel file Beyondfacts 4 2,969 Mar-22-2021, 11:51 AM
Last Post: jefsummers
  Saving text file with a click: valueerror i/o operation on closed file vizier87 5 4,422 Nov-16-2020, 07:56 AM
Last Post: Gribouillis
  saving data from text file to CSV file in python having delimiter as space K11 1 2,413 Sep-11-2020, 06:28 AM
Last Post: bowlofred
  Saving Excel workbook file with dataframe names Biplab1985 0 2,044 Jun-07-2020, 12:25 PM
Last Post: Biplab1985

Forum Jump:

User Panel Messages

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