Answers for "read write text file python"

73

python write to file

file = open(“testfile.txt”,”w”) 
 
file.write(“Hello World”) 
file.write(“This is our new text file”) 
file.write(“and this is another line.”) 
file.write(“Why? Because we can.”) 
 
file.close() 
Posted by: Guest on December-09-2019
2

how to write to a text file in python

#the way i learned it
#plus explanations

from sys import * #this is for making it more flexible

#so now we gotta open a file to write in

f = open("haha.txt", "w") # the w means WRITING
#now we gotta write something

f.write(str(argv[1])) # the str means change it into a string of letters
#argv[1] means it's the thing you type after the python run thing
#for example: "python run.py helo"

#now we gotta finish it
f.close()

#if you want to see whats in it...

f = open("haha.txt", "r") #r means READING
print(f.read()) # this prints what you wrote

#example input and output

#            python run.py helo
#   helo
Posted by: Guest on May-17-2021
3

python open and read file with

with open('pagehead.section.htm','r') as f:
    output = f.read()
Posted by: Guest on May-15-2020
1

Write a Python program to read an entire text file

# Program to read entire file
import os
PATH = "H:\py_learning\interviewsprep"
os.chdir(PATH)
def file_read(fname,mode='r+'):
    try:
        with open(fname) as txt:
            print(txt.read())
            print('>>>>>>>>>>>>>>>')
    except FileNotFoundError:
        print("check file existance in current working directory i.e : ",os.getcwd())
        print('provide file existance path to PATH variable')
    finally:
        pass
file_read('file1.txt')
Posted by: Guest on November-27-2020

Code answers related to "read write text file python"

Python Answers by Framework

Browse Popular Code Answers by Language