Page 2 - Lesson Note-DFH-Binary & CSV File
P. 2
Relative Path – Path starts from the current directory
f=open("test.txt") searches the file in current directory
Absolute Path – Path that starts from the topmost directory in the file system
f=open('E:\\temp\\test.txt') (USE DOUBLE SLASH IN ABSOLUTE PATH)
will open the above file in E drive for reading
f=open(r'E:\temp\test.txt') (BY USING r DOUBLE SLASH CAN BE IGNORED)
Standard File Streams
To work with data file, file object is required. Similarly, I/O operations from I/O devices are
done through I/O stream object.
Python performs I/O operations through input(), eval() and print() statement, so not
required to explicitly use the I/O stream objects.
The standard streams available in Python are:
i) Standard input stream
ii) Standard output stream
iii) Standard error stream
The above streams are nothing but file objects, which gets automatically connected to I/O
devices when we start Python.
To work explicitly with standard I/O stream, need to import sys module. Functions available
are write() and read().
The three standard streams are:
i) sys.stdin() – reads from standard input device
ii) sys.stdout() – writes to standard output device
iii) sys.stderr() – error messages are written to it
CSV files
CSV file is a delimited text file that uses a comma to separate values. Each line of the file is a
data record. Each record consists of one or more fields, separated by commas.
CSV (comma separated values) used to store tabular data in plain text format where the
column names and row values are separated using commas.