Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
46 views

File Input &outputs

The document discusses file input and outputs in Python. It covers opening and closing files, reading from and writing to files, file modes and operations, built-in file methods for input, output, and intra-file motion, standard files, command line arguments, the file system module, and related modules.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

File Input &outputs

The document discusses file input and outputs in Python. It covers opening and closing files, reading from and writing to files, file modes and operations, built-in file methods for input, output, and intra-file motion, standard files, command line arguments, the file system module, and related modules.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

File input &outputs

 File objects can be used not only to access normal disk files,
but also any other type of "file"that uses that abstraction.
 The open() built-in function returns a file object which is then
used for all operations.
 Operating systems like Unix even feature files as an underlying
File Objects and architectural interface for communication.
 File Built-in Function [open()]
file_object = open(file_name,access mode='r', buffering=-1)
 same file open modes used for the C library functionfopen().
 r open for read
 w open for write (truncate if necessary)
 a open for write (start at EOF, create if necessary)
 r+ open for read and write
 w+ open for read and write ("w" )
File Mode  a+ open for read and write ("a" )
Operation  rb open for binary read
 wb open for binary write ( "w")
 ab open for binary append ( "a")
 rb+ open for binary read and write ( "r+")
 wb+ open for binary read and write ( "w+")
 ab+ open for binary read and write ( "a+")
 fp = open('/etc/motd') #open file for read

examples for  fp = open('test', 'w') #open file for write

opening files  fp = open('data', 'r+') #open file for read/write


 fp = open('c:\io.sys', 'rb') #open binary file for read
 File methods come in four different categories:
 input, output, movement within a file, which we will call
"intra-file motion," and miscellaneous.
 Input:
File Built-in 1. The read() method is used to read bytes directly into a string,

Methods reading at most the number of bytesindicated.


2. readline() method reads one line of the open file
3. readinto() method reads the given number of bytes into a
writable buffer object, the same type of object returned by the
unsupported buffer() built-in function.
Output:
1. write() built-in method has the opposite functionality as read() and
readline()
2. writelines() operates on a list just like readlines(), but takes a list of
strings and writes them out to a file.
Intra-file Motion:
3. seek() method (analogous to the fseek() function in C) moves the
File Built-in Methods
file pointer to different positions within the file.
4. A value of 0 indicates distance from the beginning of a file (note
that a position measured
5. from the beginning of a file is also known as the absolute offset), a
value of 1 indicates movement from the current location in the file,
and a value of 2 indicates that the offset is from the end of the file.
6. close() method completes access to a file by closing it.
 linesep string used to separate lines in a file
 sep string used to separate file pathname components
os Module Attributes to Aid in  pathsep string used to delimit a set of file pathnames
Multi-platform Development
 curdir string name for current working directory
 pardir string name for parent (of current working directory)
filename = raw_input('Enter file name: ')
file = open(filename, 'w')
done = 0
while not done:
aLine = raw_input("Enter a line ('.’ to quit): ")

example if aLine != ".":


file.write(aLine + '\n’)
else:
done = 1
file.close()
 Three standard files which are made available to you when
your program starts. These are standard input (usually the
keyboard), standard output (buffered output to the monitor or
display), and standard error (unbuffered output to the screen).
(The "buffered"or "unbuffered" output refers to that third

Standard Files argument to open()).


 These files are named stdin, stdout, and stderr and take after
their names from the C language.
import sys
sys.stdout.write('Hello World!' + '\n)
 The sys module also provides access to any command-line
arguments via the sys.argv.
 Command-line arguments are those arguments given to the program
in addition to the script name on invocation.
 sys.argv is the list of command-line arguments
 len(sys.argv) is the number of command-line arguments (a.k.a. argc)

Command-line import sys

Arguments print 'you entered', len(sys.argv), 'arguments…'

print 'they were:', str(sys.argv)


 Here is an example invocation and output of this script:
% argv.py 76 tales 85 hawk

you entered 5 arguments…


they were: ['argv.py', '76', 'tales', '85', 'hawk']
 remove()/unlink() delete file
 rename() rename file
 *stat()[a] return file statistics
 symlink() create symbolic link
 utime() update timestamp

File System  Directories/Folders


 chdir() change working directory
 listdir() list files in directory
 getcwd() return current working directory
 mkdir()/makedirs() create directory(ies)
 rmdir()/removedirs() remove directory(ies)
 fileinput iterates over lines of multiple input text files
 getopt provides command-line argument parsing/manipulation
 glob/fnmatch provides Unix-style wildcard character matching
 gzip/zlib/zipfile[a] allows file access to include automatic
Related de/compression
Modules  shutil offers high-level file access functionality
 c/StringIO implements file-like interface on top of string
objects
 tempfile generates temporary file names or files
Thanking you!!!!!!!!

You might also like