Opening File Operation in Python
source link: http://uzairadamjee.com/blog/file-open/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Opening File Operation in PythonSkip to content
Opening File Operation in Python
This tutorial is about how to open file in python.
Syntax: open(filename, mode)
We use open () function in Python to open a file in read or write mode.
Example:
f = open("abc.txt") # open file in current directory
f = open("C:/mydocs/abc.txt") # specifying full path
Open a file by specifying a mode.
f= open("abc.txt","w+")
“w” is used for writing and will also create a file if it does not exist
file = open("myfile.txt", "r")
# a file named myfile, will be opened in reading mode
for data in file:
print (data)# This will print every line one by one in the file
We can also specify encoding type when reading a file
f = open("test.txt", mode='r', encoding='utf-8')
Leave a Reply Cancel reply
Your email address will not be published. Required fields are marked *
Comment
Name *
Email *
Website
Save my name, email, and website in this browser for the next time I comment.
Post navigation
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK