-2

I have a .txt and i want a python script to do something with it.

My .txt looks something like this:

  • 27b23815-4cbb-dfae-3e6d-38f67ec4266e
  • 81a090bd-8973-bc37-5c7b-dc1a18e8ddee
  • 7e1bf596-88bc-d8fd-9aea-278d5c689eaa
  • 0b365fb0-dea4-53a1-fd27-6cbf9721602c
  • 1c317dcf-73f4-edf5-b6a1-ad663d2b507e
  • 6db8342d-1afb-2777-1a7f-a5daad06d2db

And i want to delete the first line of the .txt if there is something writen in the 6th line but all without making a new .txt out of the old. Can't quite get the hang of this :(

I know i can delete the first Line with:

       lines = file(idpath, "r").readlines()  
       del lines[0]  
       file(idpath,"w").writelines(lines) 

Would be gratefull for any help

ivan_pozdeev
  • 33,874
  • 19
  • 107
  • 152
Scr33ni
  • 1
  • 1
  • how about you will show what have you done? – Salvador Dali Nov 25 '14 at 09:35
  • Seems more like a homework :P – vks Nov 25 '14 at 09:36
  • No, this is no homework :D. I running a XenServer and tring to delete Snapshots of my VM's. As soon as there is a 6th Snapshot i want to delete the earliest, hence I need the ID of the first Snapshot which is the first Line of the .txt. All that works besides the if statement with the 6th line. – Scr33ni Nov 25 '14 at 09:40
  • A shell language will do this more easily – Elazar Nov 25 '14 at 09:55
  • It certainly would, but i'd like to do it with Python since i have other Python scripts and i'd like to keep it to only one language – Scr33ni Nov 25 '14 at 09:58
  • so your all question is to deleted 1st line if 6th line exists, right??? – Hackaholic Nov 25 '14 at 10:06

2 Answers2

2

Since the file paradigm is a (resizable) array of bytes, you cannot simply delete data from inside. Instead, you do either of the following:

  • slide back all the data following the fragment to be deleted to overwrite it, then adjust the file's size
    (old-fashioned, typically used with memory-mapped files or when you can't afford another copy of the data)
  • write all the resulting data to a new file, then move it over the old one
    (typical in batch processing since move is atomic and instant within the same filesystem, may work (depending on the OS) even if the file is open by something else, and you can check the results before applying the change)
  • read all the data you need to preserve into memory, then truncate the file to zero bytes and write the new data
    (typical for text editors. The file cannot be open for writing by something else and the moment you truncate it, the old contents are lost. OTOH, you don't need the permissions to create another file and overwrite this one)

As for list manipulations, RTM Sequence types.

ivan_pozdeev
  • 33,874
  • 19
  • 107
  • 152
0

I figured it out! Now that i see it, it is pretty simple

 f = open('file','r')  
 lines = f.readlines()
 if lines[5]: del lines[0]
Scr33ni
  • 1
  • 1
  • `lines[5]` will raise `IndexError` if there are less than 6 lines. (the program will exit immediately so this may be of no concern to you) – ivan_pozdeev Nov 27 '14 at 12:23