Python Forum
First line of File gets deleted when reading file
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
First line of File gets deleted when reading file
#1
Hi everyone,

im quite new to python, especially to reading and writing files. 
I now wrote a script which was intended to read a "csv utf-8" file, scan a code (as a normal keyboard-input), append the current time in the list and ovwerwrite the file with the new time. Since i couldn't find another way, I let the script overwrite the whole file.

But the script is having a big issue. Whenever I overwrite the file, stop and restart the script, the first line of the table gets deleted. It must happen anytime during the second read process because the file is intact after stopping the script and opening it with excel.

In hope that you will be able to help me,

lrxM

import csv                                                                                                      #import plugins
import time

def writeTime(tableRead):
        print(tableRead[tempScan][1], tableRead[tempScan][0])
        with open("G:\SaS\IT\Alle.csv","w", newline = "") as csvfile:
                print(tableRead[0])
                presentTime = time.ctime(time.time())[11:-8]
                tableRead[tempScan].append(presentTime)
                tableRead.insert(0,"Schutzkappe")
                overwrite = csv.writer(csvfile, delimiter=';', quoting=csv.QUOTE_MINIMAL)
                overwrite.writerows(tableRead)
                print(tableRead[0])
                print(presentTime)
                print("Time was written successfully!")
                print()

with open("G:\SaS\IT\Alle.csv") as csvfile:                                                                     #file as variable, with closes the file in the memory after work is done
        read = csv.reader(csvfile, delimiter= ";")                                                              #defines the formatting, ";" seperates the single columms; quotechar isn't needed for a csv utf-8 files
        for row in read:                                                                                        #save the data into a list, acces with a[0][0] (applicable example)
                tableRead = list(read)
        print(tableRead[0])

while 1 == 1:
        print("Insert Barcode")
        tempScan = int(input())                                                                                 #most barcodescanner work as a keyboard-input
        if tempScan <= len(tableRead):
                writeTime(tableRead)
        elif tempScan == 10001:
                break
        else: print("This is not a valid code!")

print(tableRead[0])
csvfile.close()

print("Program is shutting down!")
Reply
#2
Where do you want to put the date? Every file in the system has a modified and access date attributes. You can modify them without messing with the contend of the file
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
No i dont want to modify the access time, its more of me having a list of people and wanting to save when they arrive and leave
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  "[Errno 2] No such file or directory" (.py file) IbrahimBennani 13 834 Jun-17-2024, 12:26 AM
Last Post: AdamHensley
  Reading an ASCII text file and parsing data... oradba4u 2 272 Jun-08-2024, 12:41 AM
Last Post: oradba4u
  Thread deleted ChrisOfBristol 0 376 Apr-10-2024, 02:47 AM
Last Post: ChrisOfBristol
  Reading and storing a line of output from pexpect child eagerissac 1 4,556 Feb-20-2024, 05:51 AM
Last Post: ayoshittu
  file open "file not found error" shanoger 8 1,631 Dec-14-2023, 08:03 AM
Last Post: shanoger
Sad problems with reading csv file. MassiJames 3 868 Nov-16-2023, 03:41 PM
Last Post: snippsat
  Reading in of line not working? garynewport 2 1,074 Sep-19-2023, 02:22 PM
Last Post: snippsat
  Need to replace a string with a file (HTML file) tester_V 1 935 Aug-30-2023, 03:42 AM
Last Post: Larz60+
  Reading a file name fron a folder on my desktop Fiona 4 1,117 Aug-23-2023, 11:11 AM
Last Post: Axel_Erfurt
  How can I change the uuid name of a file to his original file? MaddoxMB 2 1,158 Jul-17-2023, 10:15 PM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

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