Unit 4 Python File, Exception, Modules
Unit 4 Python File, Exception, Modules
FILES,
EXCEPTIONS
MODULES
FILE OBJECTS in PYTHON
The file object is the default and easiest
way to manipulate files in Python. It
includes a couple of methods and attributes
which make it easier for developers to read
from, and write to, files in the filesystem.
IOError exception.
The basic syntax of the open() built-in function is:
file_object =
open(file_name, access_mode='r', buffering=-1)
The file_name is a string containing the
name of the file to open. It can be a relative
or absolute/full pathname.
The access_mode optional variable is also
a string, consisting of a set of flags
indicating which mode to open the file with.
Generally, files are opened with the modes
'r,' 'w,'or 'a,' representing
Intra-file motion
seek()--
The seek() method moves the file pointer to
different positions within the file.
FileBuilt-inMethods
The seek() method sets the current file position
in a file stream.
.
whence =0 sets the reference point at the beginning of
the file
= 1 sets the reference point at the current file
position
=2 sets the reference point at the end of the file
File Processing-Functions
1) mkfifo() / mknod() —
Create named pipe/create filesystem node
File System
remove() / unlink() Delete file
rename() Rename file
stat( ) ----Return file statistics
utime() Update timestamp
tmpfile() Create and open ('w+b') new
temporary file
walk()--Generate filenames in a directory
tree
File System
Directory Functions
chdir() / fchdir()
Change working directory
chroot()
Change root directory of current process
listdir() List files in directory
File System
getcwd() / getcwdu()
Return current working directory/same but in
Unicode