builtin open function It creates a file object
built-in open() function. It creates a file object = open(file_name[, access_mode][, buffering]) access mode can be a combination of r, w, b, a and + file object attributes Attribute Description file. close returns true if file is closed, false otherwise file. mode returns access mode with which file was opened file. name returns name of the file. softspace returns false if space explicitly required with print, true otherwise
fileobject. close() flushes any unwritten information and closes the file object, after which no more writing can be done. fileobject. read([size]) reads at most size bytes from the file. less if hits EOF. fileobject. readline([size]) reads one entire line from the fileobject. write(string); note: it doesn't add newline ('n') to the end of the string
with statement with open(file_name, 'w') as f: for line in f: f. write(“Hi there”) fileobject. tell() returns the file's current position fileobject. seek(offset[, from]) set the file's current position. If from is set to 0, it means use the beginning of the file as the reference position and 1 means use the current position and if it is set to 2 then the end of the file
The os module provides a big range of useful methods to manipulate files and directories. renaming and deleting files os. rename(current_file_name, new_file_name) os. remove(file_name) directories os. mkdir(new_dir_name) os. chdir(new_dir_name) os. listdir(dir_name) os. getcwd() os. rmdir(dir_name)
- Slides: 4