delete first line in a file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Juergen Huber

    #1

    delete first line in a file

    hello,

    i am a dummy user in python and new in this programming language and so
    there will be questions, that´s for you no problem! i never have before
    programmed in any language! sorry for this! i hope you will help me, that i
    also could the basics in this language! and later many more, so i hope ! :)

    the first question for me in this newsgroup will be the follow one:

    - is there a way to delete in a csv-file the first line?!

    big thanks, juergen

  • Fredrik Lundh

    #2
    Re: delete first line in a file

    Juergen Huber wrote:
    [color=blue]
    > i am a dummy user in python and new in this programming language and so
    > there will be questions, that´s for you no problem! i never have before
    > programmed in any language! sorry for this! i hope you will help me, that i
    > also could the basics in this language! and later many more, so i hope ! :)
    >
    > the first question for me in this newsgroup will be the follow one:
    >
    > - is there a way to delete in a csv-file the first line?![/color]

    in general, modern file systems can only remove and add stuff at the end of a
    file, so the only way to do this is to rewrite the entire file.

    (you can either load the entire file, fix it up, and write it out again, or copy the
    file to a new file, skipping the first line or row).

    </F>



    Comment

    • Juergen Huber

      #3
      Re: delete first line in a file

      Fredrik Lundh wrote:[color=blue]
      > Juergen Huber wrote:
      >[color=green]
      >> i am a dummy user in python and new in this programming language and
      >> so there will be questions, that´s for you no problem! i never have
      >> before programmed in any language! sorry for this! i hope you will
      >> help me, that i also could the basics in this language! and later
      >> many more, so i hope ! :)
      >>
      >> the first question for me in this newsgroup will be the follow one:
      >>
      >> - is there a way to delete in a csv-file the first line?![/color]
      >
      > in general, modern file systems can only remove and add stuff at the
      > end of a
      > file, so the only way to do this is to rewrite the entire file.
      >
      > (you can either load the entire file, fix it up, and write it out
      > again, or copy the file to a new file, skipping the first line or
      > row).
      >
      > </F>[/color]

      ok...i thought as much, that i have to copy this file!

      how will i do that?!
      how will i fix this file => delete the first line?!

      with which commands could i do that?!

      thanks

      Comment

      • Fredrik Lundh

        #4
        Re: delete first line in a file

        Juergen Huber wrote:
        [color=blue]
        > ok...i thought as much, that i have to copy this file!
        >
        > how will i do that?!
        > how will i fix this file => delete the first line?!
        >
        > with which commands could i do that?![/color]

        start here:



        </F>



        Comment

        • Juergen Huber

          #5
          Re: delete first line in a file

          Fredrik Lundh wrote:[color=blue]
          > Juergen Huber wrote:
          >[color=green]
          >> ok...i thought as much, that i have to copy this file!
          >>
          >> how will i do that?!
          >> how will i fix this file => delete the first line?!
          >>
          >> with which commands could i do that?![/color]
          >
          > start here:
          >
          > http://docs.python.org/tut/node9.htm...00000000000000
          >
          > </F>[/color]

          that documentation i have already read, but it wouldn`t help me for my
          problem!
          i know, how i can read the first line an print them on the screen!
          but...how can i say python, delete the first line?!
          thats my problem!
          in the entry of my posting i wrote, that i am a newbie and so please
          understand me, that i ask so questions?! :-)

          thanks for your help!

          Comment

          • Michele Petrazzo

            #6
            Re: delete first line in a file

            Juergen Huber wrote:[color=blue]
            > but...how can i say python, delete the first line?!
            > thats my problem![/color]

            You can open the file, read its lines with readlines function ( that
            return a list), so make your modifies and save to another file.

            Now, do you know how to work with lists?



            and now you have only to wrote it



            Michele

            Comment

            • Schüle Daniel

              #7
              Re: delete first line in a file

              Juergen Huber schrieb:[color=blue]
              > Fredrik Lundh wrote:[color=green]
              >> Juergen Huber wrote:
              >>[color=darkred]
              >>> ok...i thought as much, that i have to copy this file!
              >>>
              >>> how will i do that?!
              >>> how will i fix this file => delete the first line?!
              >>>
              >>> with which commands could i do that?![/color]
              >> start here:
              >>
              >> http://docs.python.org/tut/node9.htm...00000000000000
              >>
              >> </F>[/color]
              >
              > that documentation i have already read, but it wouldn`t help me for my
              > problem!
              > i know, how i can read the first line an print them on the screen!
              > but...how can i say python, delete the first line?!
              > thats my problem!
              > in the entry of my posting i wrote, that i am a newbie and so please
              > understand me, that i ask so questions?! :-)
              >
              > thanks for your help![/color]


              simple!

              f = file("old.file" )
              ignore = f.readline()
              file("new.file" , "w+").write(f.r ead())

              hth, Daniel

              Comment

              • Fredrik Lundh

                #8
                Re: delete first line in a file

                Juergen Huber wrote:
                [color=blue][color=green]
                >> start here:
                >>
                >> http://docs.python.org/tut/node9.htm...00000000000000[/color]
                >
                > that documentation i have already read, but it wouldn`t help me for my
                > problem![/color]

                that documentation and a little experimentation should be all you need.
                [color=blue]
                > i know, how i can read the first line an print them on the screen!
                > but...how can i say python, delete the first line?!
                > thats my problem![/color]

                read the first line, and ignore it.

                then read all the other lines, and write them to a new file.

                </F>



                Comment

                • Juergen Huber

                  #9
                  Re: delete first line in a file

                  this helps me!

                  thank you very much!!!!!

                  greetings, juergen



                  JH> Fredrik Lundh wrote:
                  [color=blue][color=green]
                  >> Juergen Huber wrote:[/color][/color]
                  [color=blue][color=green]
                  >>[/color][/color]
                  [color=blue][color=green][color=darkred]
                  >>> ok...i thought as much, that i have to copy this file![/color][/color][/color]
                  [color=blue][color=green][color=darkred]
                  >>>[/color][/color][/color]
                  [color=blue][color=green][color=darkred]
                  >>> how will i do that?![/color][/color][/color]
                  [color=blue][color=green][color=darkred]
                  >>> how will i fix this file => delete the first line?![/color][/color][/color]
                  [color=blue][color=green][color=darkred]
                  >>>[/color][/color][/color]
                  [color=blue][color=green][color=darkred]
                  >>> with which commands could i do that?![/color][/color][/color]
                  [color=blue][color=green]
                  >>[/color][/color]
                  [color=blue][color=green]
                  >> start here:[/color][/color]
                  [color=blue][color=green]
                  >>[/color][/color]
                  [color=blue][color=green]
                  >> http://docs.python.org/tut/node9.htm...00000000000000[/color][/color]
                  [color=blue][color=green]
                  >>[/color][/color]
                  [color=blue][color=green]
                  >> </F>[/color][/color]

                  JH> that documentation i have already read, but it wouldn`t help me for my

                  JH> problem!

                  JH> i know, how i can read the first line an print them on the screen!

                  JH> but...how can i say python, delete the first line?!

                  JH> thats my problem!

                  as Fredrik told you, you can not delete the first line directly!!!

                  JH> in the entry of my posting i wrote, that i am a newbie and so please

                  JH> understand me, that i ask so questions?! :-)

                  We are trying to understand that, but it is expected some effort on

                  your side as well :)

                  You didn't read the documentation properly!

                  try to experiment with the code by yourself as well!

                  Try:

                  1) open your file

                  2) use readlines() method

                  which returns the list of ALL rows from the file and store them to some
                  variable.

                  it can be done for example like:

                  all_lines = f.readlines()

                  (if you do not know, what the list is, start here:

                  http://docs.python.org/tut/node5.htm...00000000000000)

                  4) save the list members without its first member to the file

                  for example:

                  for line in all_lines[1:]:

                  f.write(line)

                  5) close the file



                  Good luck

                  Petr Jakes











                  JH> thanks for your help!



                  Comment

                  • Petr Jakeš

                    #10
                    Re[2]: delete first line in a file

                    JH> this helps me!

                    JH> thank you very much!!!!!

                    JH> greetings, juergen
                    I am happy to see my postings (which I sent by accident to you only, not to
                    the list, see below) helped. But....:
                    Its a pity we can not see your solution (your code) here :(
                    It helps to others as well to see, how to solve some "problems"

                    Petr Jakes
                    JH>> that documentation i have already read, but it wouldn`t help me for my
                    JH>> problem!
                    JH>> i know, how i can read the first line an print them on the screen!
                    JH>> but...how can i say python, delete the first line?!
                    JH>> thats my problem!
                    PJ> as Fredrik told you, you can not delete the first line directly!!!
                    JH>> in the entry of my posting i wrote, that i am a newbie and so please
                    JH>> understand me, that i ask so questions?! :-)
                    PJ> We are trying to understand that, but it is expected some effort on
                    PJ> your side as well :)
                    PJ> You didn't read the documentation properly!
                    PJ> try to experiment with the code by yourself as well!

                    PJ> Try:

                    PJ> 1) open your file
                    PJ> 2) use readlines() method
                    PJ> which returns the list of ALL rows from the file and store them to some
                    PJ> variable.
                    PJ> it can be done for example like:

                    PJ> all_lines = f.readlines()

                    PJ> (if you do not know, what the list is, start here:
                    PJ> http://docs.python.org/tut/node5.htm...00000000000000)

                    PJ> 4) save the list members without its first member to the file
                    PJ> for example:

                    PJ> for line in all_lines[1:]:
                    PJ> f.write(line)

                    PJ> 5) close the file

                    JH> Good luck
                    JH> Petr Jakes











                    JH>> thanks for your help!




                    Comment

                    Working...