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
  Reading and storing a line of output from pexpect child eagerissac 1 4,220 Feb-20-2024, 05:51 AM
Last Post: ayoshittu
  file open "file not found error" shanoger 8 1,087 Dec-14-2023, 08:03 AM
Last Post: shanoger
Sad problems with reading csv file. MassiJames 3 618 Nov-16-2023, 03:41 PM
Last Post: snippsat
  Reading in of line not working? garynewport 2 831 Sep-19-2023, 02:22 PM
Last Post: snippsat
  Need to replace a string with a file (HTML file) tester_V 1 760 Aug-30-2023, 03:42 AM
Last Post: Larz60+
  Reading a file name fron a folder on my desktop Fiona 4 895 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 921 Jul-17-2023, 10:15 PM
Last Post: Pedroski55
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,088 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  File "<string>", line 19, in <module> error is related to what? Frankduc 9 12,540 Mar-09-2023, 07:22 AM
Last Post: LocklearSusan
  Getting last line of each line occurrence in a file tester_V 1 857 Jan-31-2023, 09:29 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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