# How to read, and print to the screen a file in python!
f =open('fileName','r')print(f.read())
f.close()# Where "fileName" is obviously the name of your file that you want to read.
#Read files with loop#Replace (File path) with your text file's pathfile=open("(File path)","r")
text =""for line infile:
text = "%s
%s"%(text, line)print(text)
withopen('filename','a')as f:# able to append data to file
f.write(var1)# Were var1 is some variable you have set previously
f.write('data')
f.close()# You can add this but it is not mandatory withopen('filename','r')as f:# able to read data from file ( also is the default mode when opening a file in python)withopen('filename','x')as f:# Creates new file, if it already exists it will cause it to failwithopen('filename','t')as f:# opens the file in text mode (also is defualt)withopen('filename','b')as f:# Use if your file will contain binary datawithopen('filename','w')as f:# Open file with ability to write, will also create the file if it does not exist (if it exists will cause it to fail)withopen('filename','+')as f:# Opens file with reading and writing# You can combine these as you like with the + for reading and writing
fileName ="file_name.txt"#Here you need to write the file name as a string
openLike =""#Here you need to write how do you want to open the file:#"w": Write, "r": Read
openedFile =open("file_name.txt", openLike)#Here you open the file
fileText = openedFile.read()#This read all the file
openedFile.close()#Close the fileprint(fileText)# Prints the file text
my_file =open("C:UsersPythonfile.txt","r")#Give the path accurately and use
text = my_file.read()print(text)#Output: The text in file.txt will be printed
# read, write, close a file# catch error if raisetry:file=open("tryCatchFile.txt","w")file.write("Hello World")file=open("tryCatchFile.txt","r")print(file.read())except Exception as e:print(e)finally:file.close()
# Open function to open the file "MyFile1.txt" # (same directory) in append mode and
file1 =open("MyFile.txt","a")# store its reference in the variable file1 # and "MyFile2.txt" in D:Text in file2
file2 =open(r"D:TextMyFile2.txt","w+")
# write data in a file.
file1 =open("SofthuntFile1.txt","w")
multiple_string =["This is Mango
","This is Apple
","This is Banana
"]# is placed to indicate EOL (End of Line)
file1.write("Hello
")
file1.writelines(multiple_string)
file1.close()#to change file access modes
file1 =open("SofthuntFile1.txt","r+")print("Output of Read function is ")print(file1.read())print()# seek(n) takes the file handle to the nth
bite from the beginning.
file1.seek(0)print("Output of Readline function is ")print(file1.readline())print()
file1.seek(0)# To show difference between read and readlineprint("Output of Read(9) function is ")print(file1.read(9))print()
file1.seek(0)print("Output of Readline(9) function is ")print(file1.readline(9))
file1.seek(0)# readlines functionprint("Output of Readlines function is ")print(file1.readlines())print()
file1.close()
>>> f =open("test.txt",'r',encoding ='utf-8')>>> f.read(4)# read the first 4 data'This'>>> f.read(4)# read the next 4 data' is '>>> f.read()# read in the rest till end of file
'my first file
This file
contains three lines
'
>>> f.read()# further reading returns empty sting''