Horje
python read file Code Example
python make txt file
file = open("text.txt", "w") 
file.write("Your text goes here") 
file.close() 
'r' open for reading (default)
'w' open for writing, truncating the file first
'x' open for exclusive creation, failing if the file already exists
'a' open for writing, appending to the end of the file if it exists
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() 
python read file
with open("file.txt", "r") as txt_file:
  return txt_file.readlines()
python read file
with open('readme.txt') as f:
    lines = f.readlines()
Code language: Python
python read file
txt = open('FILENAME.txt')
txtread = txt.read()
print(txtread)
print(txt.read())
python read file
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 file
print(fileText) # Prints the file text




15

Related
how to read a file in python Code Example how to read a file in python Code Example
rename columns pandas Code Example rename columns pandas Code Example
how to check python version Code Example how to check python version Code Example
python read json file Code Example python read json file Code Example
random number python Code Example random number python Code Example

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
8