forked from arvimal/100DaysofCode-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path24-read-write-files.py
More file actions
43 lines (39 loc) · 1.37 KB
/
24-read-write-files.py
File metadata and controls
43 lines (39 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/env python3
# Reading and writing to files.
# 1. Use the `open` builtin to create a file handler
# `readlines` is used to read all the lines in the file.
# `readlines` returns a list.
# Make sure the file descriptor is closed after the operations.
try:
fd1 = open("/tmp/new.txt")
data = fd1.readlines()
print(data)
fd1.close()
except IOError:
print("IOError while reading {0}".format(fd1.name))
# 2. Modes of access
# `open()` takes multiple modes, ie. read, write, read_write etc.
# Refer `help(open)` for details.
"""
Character Meaning
--------- ---------------------------------------------------------------
'r' open for reading (default)
'w' open for writing, truncating the file first
'x' create a new file and open it for writing
'a' open for writing, appending to the end of the file if it exists
'b' binary mode
't' text mode (default)
'+' open a disk file for updating (reading and writing)
'U' universal newline mode (deprecated)
"""
try:
fd2 = open("/tmp/new.txt", "a")
fd2.write("Hello, I am testing writes!\n")
fd2.close()
except IOError:
print("IOError while reading {0}".format(fd2.name))
# 3. Read in various chunks
# Reads can be done as
# * One line at a time.
# * Read all lines in a single go (output as a list)
# * Read the file in chunks.