FILE 2
FILE 2
All files are contained within various directories, and Python has no
problem handling these too. The os module has several methods that help
you create, remove, and change directories, they are:
a) mkdir()
b) chdir()
c) getcwd()
d) rmdir()
a) The mkdir() Method
This mkdir() method of the os module to create directories in the
current directory. You need to supply an argument to this method which
contains the name of the directory to be created.
Syntax
os.mkdir(“newdir”)
Example
Example
Syntax
os.getcwd()
Example
import os
# This would give location of the current directory
os.getcwd()
Syntax:
os.rmdir(‘dirname’)
Problem Solving And Python Programming
Example
Example
Output
Number of arguments: 4 arguments.
Note:
As mentioned above, first argument is always script name and it is
also being counted in number of arguments.
getopt.getopt method
Syntax
getopt.getopt(args, options, [long_options])
options: This is the string of option letters that the script wants
to recognize, with options that require an argument should be
followed by a colon (:).
Example
Consider we want to pass two file names through command line and
we also want to give an option to check the usage of the script. Usage of
the script is as follows.
inputfile = arg
elif opt in (“-o”, “--ofile”):
outputfile = arg
print ‘Input file is “’, inputfile
print ‘Output file is “’, outputfile
if __name__ == “__main__”:
main(sys.argv[1:])
Many programs can be run to provide you with some basic informa-
tion about how