Answers for "python file operations"

19

python write to file

with open(filename,"w") as f:
  f.write('Hello World')
Posted by: Guest on March-30-2020
7

python with file.open

# Reference https://docs.python.org/3/library/functions.html#open

# Method 1
file = open("welcome.txt", "r") # mode can be r(read) w(write) and others 
data = file.read()
file.close()

# Method 2 - automatic close
with open("welcome.txt") as infile:
  data = file.read()
Posted by: Guest on May-08-2020
11

python file open

#there are many modes you can open files in. r means read.
file = open('C:Usersyournamefilesfile.txt','r')
text = file.read()

#you can write a string to it, too!
file = open('C:Usersyournamefilesfile.txt','w')
file.write('This is a typical string')

#don't forget to close it afterwards!
file.close()
Posted by: Guest on March-26-2020
0

file methods in python

file = open(filename, filemode)
file.readable()
file.writable()
file.read()
file.readlines()
file.seek()
file.tell()
file.write()
file.close()
Posted by: Guest on September-28-2021

Python Answers by Framework

Browse Popular Code Answers by Language