-
Notifications
You must be signed in to change notification settings - Fork 3
File Handling
surendrabisht edited this page Jul 13, 2020
·
1 revision
import os
f = open(‘myfile.txt’, 'r')
firstline = f.readline()
secondline = f.readline()
print (firstline)
print (secondline)
f.close()
-
'r' mode: For reading only.
-
'w' mode: for writing only. If the specified file does not exist, it will be created.If the specified file exists, any existing data on the file will be erased.
-
'a' mode:
For appending. If the specified file does not exist, it will be created. If the specified file exist, any data written to the file is automatically added to the end
-
'r+' mode:
For both reading and writing.
for deleting file
os.remove(‘myfile.txt’)
# to rename file
os.rename(‘oldfile.txt’, ‘newfile.txt’)
there are other commands as well like mkdir, chdir(), etc.
Keep Learning. Never Settle! 😊
- Home
- 1.Introduction To Python
- 2. DataTypes
- 3. Strings and Slicing of collection
- 4.If else & Loops
- 5. Break, continue, pass
- 6. Functions
- 7. Modules
- 8. Clarifying basics
- 9. File Handling
- 10. Exception Handling
- 11. Dig into Collections
- 12. Iterators, Generators and list comprehension
- 13. lambdas , map, filter and other concepts
- 14. args, kwargs an default parameters
- 15. Functional Programming
- 16. OOPs