how to connect with files in python programming?
------------------
to communicate with files in python there are 3 steps:
1.opening a file
2.perform operations (Read or write on file)
3.close the file
what is the syntax for opening a file in python?
file object=open(file_name, access_mode)
Example:
f=open("test.txt")
syntax of closing connection with file in python:
fileObject.close()
example:
f=open("test.txt","wb") f.close()
writing in a file:
syntax:
fileObject.write(string)
f=open("test.txt","w") f.write('writing to the file line 1\n') f.write('writing to the file line 1\n') f.close()
Reading a file:
fileObject.read([size])
f=open("test.txt","r") print(f.readline()) f.close()
Renaming a file:
import os os.rename("test.txt","new.txt")
Deleting a file:
import os os.remove("test.txt")
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Files in pythons:
------------------
to communicate with files in python there are 3 steps:
1.opening a file
2.perform operations (Read or write on file)
3.close the file
what is the syntax for opening a file in python?
file object=open(file_name, access_mode)
Example:
syntax of closing connection with file in python:
fileObject.close()
example:
writing in a file:
syntax:
fileObject.write(string)
example:
Reading a file:
syntax:
fileObject.read([size])
example:
Renaming a file:
example:
Deleting a file:
example: