Updating file using PythonMost efficient way to modify the last line of a large text file in PythonHow to merge two dictionaries in a single expression?How do I check whether a file exists without exceptions?Calling an external command in PythonWhat are metaclasses in Python?How do I tell if a regular file does not exist in Bash?How to get line count cheaply in Python?Find and restore a deleted file in a Git repositoryHow to read a file line-by-line into a list?Does Python have a string 'contains' substring method?Why is reading lines from stdin much slower in C++ than Python?

How can I get people to remember my character's gender?

GitLab account hacked and repo wiped

Is Iron Man stronger than the Hulk?

no sense/need/point

Clarification of algebra in moment generating functions

Counting the Number of Real Roots of A Polynomial

In linear regression why does regularisation penalise the parameter values as well?

Why would one crossvalidate the random state number?

What is a common way to tell if an academic is "above average," or outstanding in their field? Is their h-index (Hirsh index) one of them?

Is there a word that describes the unjustified use of a more complex word?

Is space itself expanding or is it just momentum from the Big Bang carrying things apart?

Can full drive backup be used instead of MSSQL database backup?

When did England stop being a Papal fief?

Can my 2 children, aged 10 and 12, who are US citizens, travel to the USA on expired American passports?

How to Practice After Stream Entry as Opposed to Before?

Krull dimension of the ring of global sections

Has the Hulk always been able to talk?

Why would a military not separate its forces into different branches?

weird pluperfect subjunctive in Eutropius

How do I allocate more memory to an app on Sheepshaver running Mac OS 9?

What are the requirements for a river delta to form?

Constitutional limitation of criminalizing behavior in US law?

How can a hefty sand storm happen in a thin atmosphere like Martian?

Dihedral group D4 composition with custom labels



Updating file using Python


Most efficient way to modify the last line of a large text file in PythonHow to merge two dictionaries in a single expression?How do I check whether a file exists without exceptions?Calling an external command in PythonWhat are metaclasses in Python?How do I tell if a regular file does not exist in Bash?How to get line count cheaply in Python?Find and restore a deleted file in a Git repositoryHow to read a file line-by-line into a list?Does Python have a string 'contains' substring method?Why is reading lines from stdin much slower in C++ than Python?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















I know the normal way to write to a file by rewriting the lines except the ones to delete. But I want to know is there an efficient way to delete or update a line in place or append at the last in a file using file pointers in Python.










share|improve this question




























    1















    I know the normal way to write to a file by rewriting the lines except the ones to delete. But I want to know is there an efficient way to delete or update a line in place or append at the last in a file using file pointers in Python.










    share|improve this question
























      1












      1








      1








      I know the normal way to write to a file by rewriting the lines except the ones to delete. But I want to know is there an efficient way to delete or update a line in place or append at the last in a file using file pointers in Python.










      share|improve this question














      I know the normal way to write to a file by rewriting the lines except the ones to delete. But I want to know is there an efficient way to delete or update a line in place or append at the last in a file using file pointers in Python.







      python python-3.x file-io file-handling






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 23 at 3:21









      Paras KarnawatParas Karnawat

      13




      13






















          1 Answer
          1






          active

          oldest

          votes


















          0














          Appending to the end is easy:



          with open('somefile', 'a') as f:
          f.write(line) # Or with print to add a newline for you, print(line, file=f)


          In the middle, you're generally stuck; unless the new line is exactly the same length as the existing line, you'll have to move all the data after that line around to make it work, and that risks data corruption if anything (including non-software issues like a power outage) goes wrong. In that case, just write a new file, and use os.replace to atomically replace the old file with the new file after the new file is written out completely.






          share|improve this answer























          • I want to know if there's a way to delete a line without rewriting the whole file except that line.

            – Paras Karnawat
            Mar 23 at 3:29











          • @ParasKarnawat: Unless the line is at the end of the file, you're going to have to rewrite every byte from that line onward. And as I said, doing so in a crash-safe way is effectively impossible (barring file system transactional write APIs, which Python doesn't give you access to even if they exist), so rewriting is the only way to do this safely.

            – ShadowRanger
            Mar 23 at 3:39











          • Alright, thanks a lot!

            – Paras Karnawat
            Mar 23 at 3:41











          • @ParasKarnawat: There are solutions that work for replacing lines at the end of a file, where at least you're not stuck with a massive copy down operation, but even there you risk data corruption if you're interrupted in the middle.

            – ShadowRanger
            Mar 23 at 3:43











          • Can you explain that way, I just want to try it and implement it to see how it works?

            – Paras Karnawat
            Mar 23 at 3:44











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55310312%2fupdating-file-using-python%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          Appending to the end is easy:



          with open('somefile', 'a') as f:
          f.write(line) # Or with print to add a newline for you, print(line, file=f)


          In the middle, you're generally stuck; unless the new line is exactly the same length as the existing line, you'll have to move all the data after that line around to make it work, and that risks data corruption if anything (including non-software issues like a power outage) goes wrong. In that case, just write a new file, and use os.replace to atomically replace the old file with the new file after the new file is written out completely.






          share|improve this answer























          • I want to know if there's a way to delete a line without rewriting the whole file except that line.

            – Paras Karnawat
            Mar 23 at 3:29











          • @ParasKarnawat: Unless the line is at the end of the file, you're going to have to rewrite every byte from that line onward. And as I said, doing so in a crash-safe way is effectively impossible (barring file system transactional write APIs, which Python doesn't give you access to even if they exist), so rewriting is the only way to do this safely.

            – ShadowRanger
            Mar 23 at 3:39











          • Alright, thanks a lot!

            – Paras Karnawat
            Mar 23 at 3:41











          • @ParasKarnawat: There are solutions that work for replacing lines at the end of a file, where at least you're not stuck with a massive copy down operation, but even there you risk data corruption if you're interrupted in the middle.

            – ShadowRanger
            Mar 23 at 3:43











          • Can you explain that way, I just want to try it and implement it to see how it works?

            – Paras Karnawat
            Mar 23 at 3:44















          0














          Appending to the end is easy:



          with open('somefile', 'a') as f:
          f.write(line) # Or with print to add a newline for you, print(line, file=f)


          In the middle, you're generally stuck; unless the new line is exactly the same length as the existing line, you'll have to move all the data after that line around to make it work, and that risks data corruption if anything (including non-software issues like a power outage) goes wrong. In that case, just write a new file, and use os.replace to atomically replace the old file with the new file after the new file is written out completely.






          share|improve this answer























          • I want to know if there's a way to delete a line without rewriting the whole file except that line.

            – Paras Karnawat
            Mar 23 at 3:29











          • @ParasKarnawat: Unless the line is at the end of the file, you're going to have to rewrite every byte from that line onward. And as I said, doing so in a crash-safe way is effectively impossible (barring file system transactional write APIs, which Python doesn't give you access to even if they exist), so rewriting is the only way to do this safely.

            – ShadowRanger
            Mar 23 at 3:39











          • Alright, thanks a lot!

            – Paras Karnawat
            Mar 23 at 3:41











          • @ParasKarnawat: There are solutions that work for replacing lines at the end of a file, where at least you're not stuck with a massive copy down operation, but even there you risk data corruption if you're interrupted in the middle.

            – ShadowRanger
            Mar 23 at 3:43











          • Can you explain that way, I just want to try it and implement it to see how it works?

            – Paras Karnawat
            Mar 23 at 3:44













          0












          0








          0







          Appending to the end is easy:



          with open('somefile', 'a') as f:
          f.write(line) # Or with print to add a newline for you, print(line, file=f)


          In the middle, you're generally stuck; unless the new line is exactly the same length as the existing line, you'll have to move all the data after that line around to make it work, and that risks data corruption if anything (including non-software issues like a power outage) goes wrong. In that case, just write a new file, and use os.replace to atomically replace the old file with the new file after the new file is written out completely.






          share|improve this answer













          Appending to the end is easy:



          with open('somefile', 'a') as f:
          f.write(line) # Or with print to add a newline for you, print(line, file=f)


          In the middle, you're generally stuck; unless the new line is exactly the same length as the existing line, you'll have to move all the data after that line around to make it work, and that risks data corruption if anything (including non-software issues like a power outage) goes wrong. In that case, just write a new file, and use os.replace to atomically replace the old file with the new file after the new file is written out completely.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 23 at 3:28









          ShadowRangerShadowRanger

          65.2k664103




          65.2k664103












          • I want to know if there's a way to delete a line without rewriting the whole file except that line.

            – Paras Karnawat
            Mar 23 at 3:29











          • @ParasKarnawat: Unless the line is at the end of the file, you're going to have to rewrite every byte from that line onward. And as I said, doing so in a crash-safe way is effectively impossible (barring file system transactional write APIs, which Python doesn't give you access to even if they exist), so rewriting is the only way to do this safely.

            – ShadowRanger
            Mar 23 at 3:39











          • Alright, thanks a lot!

            – Paras Karnawat
            Mar 23 at 3:41











          • @ParasKarnawat: There are solutions that work for replacing lines at the end of a file, where at least you're not stuck with a massive copy down operation, but even there you risk data corruption if you're interrupted in the middle.

            – ShadowRanger
            Mar 23 at 3:43











          • Can you explain that way, I just want to try it and implement it to see how it works?

            – Paras Karnawat
            Mar 23 at 3:44

















          • I want to know if there's a way to delete a line without rewriting the whole file except that line.

            – Paras Karnawat
            Mar 23 at 3:29











          • @ParasKarnawat: Unless the line is at the end of the file, you're going to have to rewrite every byte from that line onward. And as I said, doing so in a crash-safe way is effectively impossible (barring file system transactional write APIs, which Python doesn't give you access to even if they exist), so rewriting is the only way to do this safely.

            – ShadowRanger
            Mar 23 at 3:39











          • Alright, thanks a lot!

            – Paras Karnawat
            Mar 23 at 3:41











          • @ParasKarnawat: There are solutions that work for replacing lines at the end of a file, where at least you're not stuck with a massive copy down operation, but even there you risk data corruption if you're interrupted in the middle.

            – ShadowRanger
            Mar 23 at 3:43











          • Can you explain that way, I just want to try it and implement it to see how it works?

            – Paras Karnawat
            Mar 23 at 3:44
















          I want to know if there's a way to delete a line without rewriting the whole file except that line.

          – Paras Karnawat
          Mar 23 at 3:29





          I want to know if there's a way to delete a line without rewriting the whole file except that line.

          – Paras Karnawat
          Mar 23 at 3:29













          @ParasKarnawat: Unless the line is at the end of the file, you're going to have to rewrite every byte from that line onward. And as I said, doing so in a crash-safe way is effectively impossible (barring file system transactional write APIs, which Python doesn't give you access to even if they exist), so rewriting is the only way to do this safely.

          – ShadowRanger
          Mar 23 at 3:39





          @ParasKarnawat: Unless the line is at the end of the file, you're going to have to rewrite every byte from that line onward. And as I said, doing so in a crash-safe way is effectively impossible (barring file system transactional write APIs, which Python doesn't give you access to even if they exist), so rewriting is the only way to do this safely.

          – ShadowRanger
          Mar 23 at 3:39













          Alright, thanks a lot!

          – Paras Karnawat
          Mar 23 at 3:41





          Alright, thanks a lot!

          – Paras Karnawat
          Mar 23 at 3:41













          @ParasKarnawat: There are solutions that work for replacing lines at the end of a file, where at least you're not stuck with a massive copy down operation, but even there you risk data corruption if you're interrupted in the middle.

          – ShadowRanger
          Mar 23 at 3:43





          @ParasKarnawat: There are solutions that work for replacing lines at the end of a file, where at least you're not stuck with a massive copy down operation, but even there you risk data corruption if you're interrupted in the middle.

          – ShadowRanger
          Mar 23 at 3:43













          Can you explain that way, I just want to try it and implement it to see how it works?

          – Paras Karnawat
          Mar 23 at 3:44





          Can you explain that way, I just want to try it and implement it to see how it works?

          – Paras Karnawat
          Mar 23 at 3:44



















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55310312%2fupdating-file-using-python%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

          SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

          은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현