PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, May 13, 2022

[FIXED] How do I append text to a file with python?

 May 13, 2022     append, file, python     No comments   

Issue

I am trying to make a file that I can continuously add '../' to. My code is as follows:

with open("/tmp/newfile.txt", "a+") as myfile:
  myfile.write('../')
  contents = myfile.read()
  print(contents)

However, when I run this code it returns <empty>


Solution

For Append File:

with open("newfile.txt", "a+") as file:
    file.write("I am adding in more lines\n")
    file.write("And more…")

For Read File:

with open('newfile.txt') as f:
    lines = f.readlines()
     print(lines)


Answered By - Kayes Fahim
Answer Checked By - Gilberto Lyons (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing