SlideShare une entreprise Scribd logo
1  sur  20
http://www.skillbrew.com
/SkillbrewTalent brewed by the industry itself
os and sys Modules
Pavan Verma
@YinYangPavan
1
Founder, P3 InfoTech Solutions Pvt. Ltd.
Python Programming Essentials
© SkillBrew http://skillbrew.com
os module introduction
 os module provides a unified interface to a number
of operating system functions
 Most of the functions in this module are
implemented by platform specific modules, such as
posix and nt. The os module automatically loads the
right implementation module when it is first
imported.
2
© SkillBrew http://skillbrew.com
Working with files and directories
Functionality Description
os.rename(src, dst) Rename the file or directory src to dst.
os.remove(path) Remove (delete) the file path. If path is a
directory, OSError is raised
os.mkdir(path) Creates a Directory named path
os.rmdir(path) Remove (delete) the directory path. Only
works when the directory is empty.
3
© SkillBrew http://skillbrew.com
Working with files and directories (2)
Functionality Description
os.listdir(path) Return a list containing the names of the
entries in the directory given by path
os.chdir(path) Change the current working directory to
path
os.getcwd() Return a string representing the current
working directory
os.stat(path) Perform the equivalent of a stat() system
call on the given path
os.symlink(source,li
nk_name)
Create a symbolic link pointing to source
named link_name.(unix only)
4
© SkillBrew http://skillbrew.com
getcwd()
5
import os
curr_dir = os.getcwd()
print curr_dir
Output:
C:UsersPavanPycharmProjectsskillbrew programs
returns a string representing the current working directory
© SkillBrew http://skillbrew.com
Make and change directories
6
import os
curr_dir = os.getcwd()
print curr_dir
os.mkdir(os.path.join(curr_dir, 'foo'))
os.chdir(os.path.join(curr_dir, 'foo'))
print os.getcwd()
Output:
C:UsersPavanPycharmProjectsskillbrew programs
C:UsersPavanPycharmProjectsskillbrew programsfoo
© SkillBrew http://skillbrew.com
List all files in directory
7
import os
print os.listdir(os.getcwd())
returns a list containing the names of the entries in the
directory given by path
© SkillBrew http://skillbrew.com
os.path – Common pathname manipulations
 split
 splitext
 join
8
© SkillBrew http://skillbrew.com
split
9
>>> import os
>>>os.path.split("/home/user/pictures/foo.jpg")
('/home/user/pictures', 'foo.jpg')
>>> filepath, filename = os.path.split(
"/home/user/pictures/foo.jpg")
>>> filepath
'/home/user/pictures'
>>> filename
'foo.jpg'
split function splits a full pathname and returns a
tuple containing the path and filename
© SkillBrew http://skillbrew.com
splittext
10
>>> import os
>>> os.path.splitext('foo.jpg')
('foo', '.jpg')
splitext splits a filename and returns a tuple containing
the filename and the file extension
© SkillBrew http://skillbrew.com
join
11
>>> import os
>>>filepath, filename = os.path.split(
"/home/user/pictures/foo.jpg")
>>> print os.path.join(filepath, filename)
Output:
/home/user/pictures/foo.jpg
The join function of os.path constructs a pathname out
of one or more partial pathnames
© SkillBrew http://skillbrew.com
Sys module
12
© SkillBrew http://skillbrew.com
sys module
sys module provides a number of functions and
variables that can be used to manipulate different
parts of the Python runtime environment
13
© SkillBrew http://skillbrew.com
command-line arguments
The argv list contains the arguments passed to the script, when
the interpreter was started. The first item contains the name of
the script itself.
14
import sys
print "script name: %s" % sys.argv[0]
print len(sys.argv)
$ python sys_ex1.py
Output:
Script name: sys_ex1.py
1
© SkillBrew http://skillbrew.com
import sys
if len(sys.argv) < 3:
print "You have to enter minimum 2 arguments"
sys.exit(1)
print "The arguments are:"
for arg in sys.argv:
print arg
$ python sys_ex1.py
Output:
You have to enter minimum 2 arguments
command-line arguments (2)
15
© SkillBrew http://skillbrew.com
import sys
if len(sys.argv) < 3:
print "You have to enter minimum 2 arguments"
sys.exit(1)
print "The arguments are:"
for arg in sys.argv:
print arg
$ python sys_ex1.py foo bar
Output:
The arguments are:
sys_ex1.py
foo
bar
command-line arguments (2)
16
© SkillBrew http://skillbrew.com
Exiting the program
 When you reach the end of the main program, the
interpreter is automatically terminated
 If you need to exit in midflight, you can call
the sys.exit function instead
 This function takes an optional integer value, which is
returned to the calling program
 If it is an integer, zero is considered "successful
termination" and any nonzero value is considered
"abnormal termination"
17
© SkillBrew http://skillbrew.com
Exiting the program (2)
18
import sys
if len(sys.argv) < 3:
print "You have to enter minimum 2 arguments"
sys.exit(1)
print "The arguments are:"
for arg in sys.argv:
print arg
© SkillBrew http://skillbrew.com
Summary
 os module introduction
 Working with files and directories
 Make and change directories
 List all files in directories
 Pathname manipulations
 sys module introduction
 Command line arguments
 Exiting the program
19
© SkillBrew http://skillbrew.com
References
 Sys module details http://effbot.org/librarybook/sys.htm
 http://docs.python.org/2/library/sys.html
 http://docs.python.org/2/library/os.html#os-file-dir
 http://docs.python.org/2/library/os.html
20

Contenu connexe

Tendances

Looping Statements and Control Statements in Python
Looping Statements and Control Statements in PythonLooping Statements and Control Statements in Python
Looping Statements and Control Statements in PythonPriyankaC44
 
Regular expressions in Python
Regular expressions in PythonRegular expressions in Python
Regular expressions in PythonSujith Kumar
 
Looping statement in python
Looping statement in pythonLooping statement in python
Looping statement in pythonRaginiJain21
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.pptTareq Hasan
 
Variables in python
Variables in pythonVariables in python
Variables in pythonJaya Kumari
 
Python programming introduction
Python programming introductionPython programming introduction
Python programming introductionSiddique Ibrahim
 

Tendances (20)

Python libraries
Python librariesPython libraries
Python libraries
 
Python Programming Essentials - M9 - String Formatting
Python Programming Essentials - M9 - String FormattingPython Programming Essentials - M9 - String Formatting
Python Programming Essentials - M9 - String Formatting
 
Python Tutorial
Python TutorialPython Tutorial
Python Tutorial
 
Python file handling
Python file handlingPython file handling
Python file handling
 
Looping Statements and Control Statements in Python
Looping Statements and Control Statements in PythonLooping Statements and Control Statements in Python
Looping Statements and Control Statements in Python
 
Regular expressions in Python
Regular expressions in PythonRegular expressions in Python
Regular expressions in Python
 
Looping statement in python
Looping statement in pythonLooping statement in python
Looping statement in python
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt
 
Python programming : Threads
Python programming : ThreadsPython programming : Threads
Python programming : Threads
 
Python programming : Classes objects
Python programming : Classes objectsPython programming : Classes objects
Python programming : Classes objects
 
Python exception handling
Python   exception handlingPython   exception handling
Python exception handling
 
Python : Functions
Python : FunctionsPython : Functions
Python : Functions
 
Variables in python
Variables in pythonVariables in python
Variables in python
 
Python programming introduction
Python programming introductionPython programming introduction
Python programming introduction
 
File handling in c
File handling in cFile handling in c
File handling in c
 
Python cgi programming
Python cgi programmingPython cgi programming
Python cgi programming
 
Python Modules
Python ModulesPython Modules
Python Modules
 
Operators in python
Operators in pythonOperators in python
Operators in python
 
Sets in python
Sets in pythonSets in python
Sets in python
 
Python Functions
Python   FunctionsPython   Functions
Python Functions
 

En vedette

Python Programming Essentials - M37 - Brief Overview of Misc Concepts
Python Programming Essentials - M37 - Brief Overview of Misc ConceptsPython Programming Essentials - M37 - Brief Overview of Misc Concepts
Python Programming Essentials - M37 - Brief Overview of Misc ConceptsP3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M29 - Python Interpreter and Files
Python Programming Essentials - M29 - Python Interpreter and FilesPython Programming Essentials - M29 - Python Interpreter and Files
Python Programming Essentials - M29 - Python Interpreter and FilesP3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M35 - Iterators & Generators
Python Programming Essentials - M35 - Iterators & GeneratorsPython Programming Essentials - M35 - Iterators & Generators
Python Programming Essentials - M35 - Iterators & GeneratorsP3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M40 - Invoking External Programs
Python Programming Essentials - M40 - Invoking External ProgramsPython Programming Essentials - M40 - Invoking External Programs
Python Programming Essentials - M40 - Invoking External ProgramsP3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M28 - Debugging with pdb
Python Programming Essentials - M28 - Debugging with pdbPython Programming Essentials - M28 - Debugging with pdb
Python Programming Essentials - M28 - Debugging with pdbP3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M34 - List Comprehensions
Python Programming Essentials - M34 - List ComprehensionsPython Programming Essentials - M34 - List Comprehensions
Python Programming Essentials - M34 - List ComprehensionsP3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M44 - Overview of Web Development
Python Programming Essentials - M44 - Overview of Web DevelopmentPython Programming Essentials - M44 - Overview of Web Development
Python Programming Essentials - M44 - Overview of Web DevelopmentP3 InfoTech Solutions Pvt. Ltd.
 

En vedette (12)

Docopt
DocoptDocopt
Docopt
 
Python Programming Essentials - M37 - Brief Overview of Misc Concepts
Python Programming Essentials - M37 - Brief Overview of Misc ConceptsPython Programming Essentials - M37 - Brief Overview of Misc Concepts
Python Programming Essentials - M37 - Brief Overview of Misc Concepts
 
Python Programming Essentials - M29 - Python Interpreter and Files
Python Programming Essentials - M29 - Python Interpreter and FilesPython Programming Essentials - M29 - Python Interpreter and Files
Python Programming Essentials - M29 - Python Interpreter and Files
 
Python Programming Essentials - M35 - Iterators & Generators
Python Programming Essentials - M35 - Iterators & GeneratorsPython Programming Essentials - M35 - Iterators & Generators
Python Programming Essentials - M35 - Iterators & Generators
 
Python Programming Essentials - M39 - Unit Testing
Python Programming Essentials - M39 - Unit TestingPython Programming Essentials - M39 - Unit Testing
Python Programming Essentials - M39 - Unit Testing
 
Python Programming Essentials - M40 - Invoking External Programs
Python Programming Essentials - M40 - Invoking External ProgramsPython Programming Essentials - M40 - Invoking External Programs
Python Programming Essentials - M40 - Invoking External Programs
 
Python Programming Essentials - M28 - Debugging with pdb
Python Programming Essentials - M28 - Debugging with pdbPython Programming Essentials - M28 - Debugging with pdb
Python Programming Essentials - M28 - Debugging with pdb
 
Python Programming Essentials - M31 - PEP 8
Python Programming Essentials - M31 - PEP 8Python Programming Essentials - M31 - PEP 8
Python Programming Essentials - M31 - PEP 8
 
Python Programming Essentials - M34 - List Comprehensions
Python Programming Essentials - M34 - List ComprehensionsPython Programming Essentials - M34 - List Comprehensions
Python Programming Essentials - M34 - List Comprehensions
 
Python Programming Essentials - M5 - Variables
Python Programming Essentials - M5 - VariablesPython Programming Essentials - M5 - Variables
Python Programming Essentials - M5 - Variables
 
Python Programming Essentials - M44 - Overview of Web Development
Python Programming Essentials - M44 - Overview of Web DevelopmentPython Programming Essentials - M44 - Overview of Web Development
Python Programming Essentials - M44 - Overview of Web Development
 
Python Programming Essentials - M27 - Logging module
Python Programming Essentials - M27 - Logging modulePython Programming Essentials - M27 - Logging module
Python Programming Essentials - M27 - Logging module
 

Similaire à OS and sys modules in Python

LOSS_C11- Programming Linux 20221006.pdf
LOSS_C11- Programming Linux 20221006.pdfLOSS_C11- Programming Linux 20221006.pdf
LOSS_C11- Programming Linux 20221006.pdfThninh2
 
Dependencies Managers in C/C++. Using stdcpp 2014
Dependencies Managers in C/C++. Using stdcpp 2014Dependencies Managers in C/C++. Using stdcpp 2014
Dependencies Managers in C/C++. Using stdcpp 2014biicode
 
(1) cpp introducing the_cpp_programming_language
(1) cpp introducing the_cpp_programming_language(1) cpp introducing the_cpp_programming_language
(1) cpp introducing the_cpp_programming_languageNico Ludwig
 
ARGUS - THE OMNISCIENT CI
ARGUS - THE OMNISCIENT CIARGUS - THE OMNISCIENT CI
ARGUS - THE OMNISCIENT CICosmin Poieana
 
C Under Linux
C Under LinuxC Under Linux
C Under Linuxmohan43u
 
2009 cluster user training
2009 cluster user training2009 cluster user training
2009 cluster user trainingChris Dwan
 
Frontend JS workflow - Gulp 4 and the like
Frontend JS workflow - Gulp 4 and the likeFrontend JS workflow - Gulp 4 and the like
Frontend JS workflow - Gulp 4 and the likeDamien Seguin
 
Embedded Android
Embedded AndroidEmbedded Android
Embedded Android晓东 杜
 
Puppet at Opera Sofware - PuppetCamp Oslo 2013
Puppet at Opera Sofware - PuppetCamp Oslo 2013Puppet at Opera Sofware - PuppetCamp Oslo 2013
Puppet at Opera Sofware - PuppetCamp Oslo 2013Cosimo Streppone
 
Lex tool manual
Lex tool manualLex tool manual
Lex tool manualSami Said
 
Debugging Python with gdb
Debugging Python with gdbDebugging Python with gdb
Debugging Python with gdbRoman Podoliaka
 
NASM Introduction.pptx
NASM Introduction.pptxNASM Introduction.pptx
NASM Introduction.pptxAnshKarwa
 
Course 102: Lecture 8: Composite Commands
Course 102: Lecture 8: Composite Commands Course 102: Lecture 8: Composite Commands
Course 102: Lecture 8: Composite Commands Ahmed El-Arabawy
 

Similaire à OS and sys modules in Python (20)

LOSS_C11- Programming Linux 20221006.pdf
LOSS_C11- Programming Linux 20221006.pdfLOSS_C11- Programming Linux 20221006.pdf
LOSS_C11- Programming Linux 20221006.pdf
 
Dependencies Managers in C/C++. Using stdcpp 2014
Dependencies Managers in C/C++. Using stdcpp 2014Dependencies Managers in C/C++. Using stdcpp 2014
Dependencies Managers in C/C++. Using stdcpp 2014
 
(1) cpp introducing the_cpp_programming_language
(1) cpp introducing the_cpp_programming_language(1) cpp introducing the_cpp_programming_language
(1) cpp introducing the_cpp_programming_language
 
ARGUS - THE OMNISCIENT CI
ARGUS - THE OMNISCIENT CIARGUS - THE OMNISCIENT CI
ARGUS - THE OMNISCIENT CI
 
C Under Linux
C Under LinuxC Under Linux
C Under Linux
 
2009 cluster user training
2009 cluster user training2009 cluster user training
2009 cluster user training
 
Frontend JS workflow - Gulp 4 and the like
Frontend JS workflow - Gulp 4 and the likeFrontend JS workflow - Gulp 4 and the like
Frontend JS workflow - Gulp 4 and the like
 
Embedded Android
Embedded AndroidEmbedded Android
Embedded Android
 
Puppet at Opera Sofware - PuppetCamp Oslo 2013
Puppet at Opera Sofware - PuppetCamp Oslo 2013Puppet at Opera Sofware - PuppetCamp Oslo 2013
Puppet at Opera Sofware - PuppetCamp Oslo 2013
 
Os lab final
Os lab finalOs lab final
Os lab final
 
Lex tool manual
Lex tool manualLex tool manual
Lex tool manual
 
Linux
LinuxLinux
Linux
 
Linux
LinuxLinux
Linux
 
Linux
LinuxLinux
Linux
 
Linux
LinuxLinux
Linux
 
lec4.docx
lec4.docxlec4.docx
lec4.docx
 
Debugging Python with gdb
Debugging Python with gdbDebugging Python with gdb
Debugging Python with gdb
 
NASM Introduction.pptx
NASM Introduction.pptxNASM Introduction.pptx
NASM Introduction.pptx
 
Course 102: Lecture 8: Composite Commands
Course 102: Lecture 8: Composite Commands Course 102: Lecture 8: Composite Commands
Course 102: Lecture 8: Composite Commands
 
Linux com
Linux comLinux com
Linux com
 

Plus de P3 InfoTech Solutions Pvt. Ltd.

Python Programming Essentials - M21 - Exception Handling
Python Programming Essentials - M21 - Exception HandlingPython Programming Essentials - M21 - Exception Handling
Python Programming Essentials - M21 - Exception HandlingP3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M20 - Classes and Objects
Python Programming Essentials - M20 - Classes and ObjectsPython Programming Essentials - M20 - Classes and Objects
Python Programming Essentials - M20 - Classes and ObjectsP3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M19 - Namespaces, Global Variables and Docstr...
Python Programming Essentials - M19 - Namespaces, Global Variables and Docstr...Python Programming Essentials - M19 - Namespaces, Global Variables and Docstr...
Python Programming Essentials - M19 - Namespaces, Global Variables and Docstr...P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M18 - Modules and Packages
Python Programming Essentials - M18 - Modules and PackagesPython Programming Essentials - M18 - Modules and Packages
Python Programming Essentials - M18 - Modules and PackagesP3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M16 - Control Flow Statements and Loops
Python Programming Essentials - M16 - Control Flow Statements and LoopsPython Programming Essentials - M16 - Control Flow Statements and Loops
Python Programming Essentials - M16 - Control Flow Statements and LoopsP3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M11 - Comparison and Logical Operators
Python Programming Essentials - M11 - Comparison and Logical OperatorsPython Programming Essentials - M11 - Comparison and Logical Operators
Python Programming Essentials - M11 - Comparison and Logical OperatorsP3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M10 - Numbers and Artihmetic Operators
Python Programming Essentials - M10 - Numbers and Artihmetic OperatorsPython Programming Essentials - M10 - Numbers and Artihmetic Operators
Python Programming Essentials - M10 - Numbers and Artihmetic OperatorsP3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M6 - Code Blocks and Indentation
Python Programming Essentials - M6 - Code Blocks and IndentationPython Programming Essentials - M6 - Code Blocks and Indentation
Python Programming Essentials - M6 - Code Blocks and IndentationP3 InfoTech Solutions Pvt. Ltd.
 

Plus de P3 InfoTech Solutions Pvt. Ltd. (17)

Python Programming Essentials - M24 - math module
Python Programming Essentials - M24 - math modulePython Programming Essentials - M24 - math module
Python Programming Essentials - M24 - math module
 
Python Programming Essentials - M23 - datetime module
Python Programming Essentials - M23 - datetime modulePython Programming Essentials - M23 - datetime module
Python Programming Essentials - M23 - datetime module
 
Python Programming Essentials - M22 - File Operations
Python Programming Essentials - M22 - File OperationsPython Programming Essentials - M22 - File Operations
Python Programming Essentials - M22 - File Operations
 
Python Programming Essentials - M21 - Exception Handling
Python Programming Essentials - M21 - Exception HandlingPython Programming Essentials - M21 - Exception Handling
Python Programming Essentials - M21 - Exception Handling
 
Python Programming Essentials - M20 - Classes and Objects
Python Programming Essentials - M20 - Classes and ObjectsPython Programming Essentials - M20 - Classes and Objects
Python Programming Essentials - M20 - Classes and Objects
 
Python Programming Essentials - M19 - Namespaces, Global Variables and Docstr...
Python Programming Essentials - M19 - Namespaces, Global Variables and Docstr...Python Programming Essentials - M19 - Namespaces, Global Variables and Docstr...
Python Programming Essentials - M19 - Namespaces, Global Variables and Docstr...
 
Python Programming Essentials - M18 - Modules and Packages
Python Programming Essentials - M18 - Modules and PackagesPython Programming Essentials - M18 - Modules and Packages
Python Programming Essentials - M18 - Modules and Packages
 
Python Programming Essentials - M17 - Functions
Python Programming Essentials - M17 - FunctionsPython Programming Essentials - M17 - Functions
Python Programming Essentials - M17 - Functions
 
Python Programming Essentials - M16 - Control Flow Statements and Loops
Python Programming Essentials - M16 - Control Flow Statements and LoopsPython Programming Essentials - M16 - Control Flow Statements and Loops
Python Programming Essentials - M16 - Control Flow Statements and Loops
 
Python Programming Essentials - M15 - References
Python Programming Essentials - M15 - ReferencesPython Programming Essentials - M15 - References
Python Programming Essentials - M15 - References
 
Python Programming Essentials - M14 - Dictionaries
Python Programming Essentials - M14 - DictionariesPython Programming Essentials - M14 - Dictionaries
Python Programming Essentials - M14 - Dictionaries
 
Python Programming Essentials - M13 - Tuples
Python Programming Essentials - M13 - TuplesPython Programming Essentials - M13 - Tuples
Python Programming Essentials - M13 - Tuples
 
Python Programming Essentials - M12 - Lists
Python Programming Essentials - M12 - ListsPython Programming Essentials - M12 - Lists
Python Programming Essentials - M12 - Lists
 
Python Programming Essentials - M11 - Comparison and Logical Operators
Python Programming Essentials - M11 - Comparison and Logical OperatorsPython Programming Essentials - M11 - Comparison and Logical Operators
Python Programming Essentials - M11 - Comparison and Logical Operators
 
Python Programming Essentials - M10 - Numbers and Artihmetic Operators
Python Programming Essentials - M10 - Numbers and Artihmetic OperatorsPython Programming Essentials - M10 - Numbers and Artihmetic Operators
Python Programming Essentials - M10 - Numbers and Artihmetic Operators
 
Python Programming Essentials - M7 - Strings
Python Programming Essentials - M7 - StringsPython Programming Essentials - M7 - Strings
Python Programming Essentials - M7 - Strings
 
Python Programming Essentials - M6 - Code Blocks and Indentation
Python Programming Essentials - M6 - Code Blocks and IndentationPython Programming Essentials - M6 - Code Blocks and Indentation
Python Programming Essentials - M6 - Code Blocks and Indentation
 

OS and sys modules in Python

  • 1. http://www.skillbrew.com /SkillbrewTalent brewed by the industry itself os and sys Modules Pavan Verma @YinYangPavan 1 Founder, P3 InfoTech Solutions Pvt. Ltd. Python Programming Essentials
  • 2. © SkillBrew http://skillbrew.com os module introduction  os module provides a unified interface to a number of operating system functions  Most of the functions in this module are implemented by platform specific modules, such as posix and nt. The os module automatically loads the right implementation module when it is first imported. 2
  • 3. © SkillBrew http://skillbrew.com Working with files and directories Functionality Description os.rename(src, dst) Rename the file or directory src to dst. os.remove(path) Remove (delete) the file path. If path is a directory, OSError is raised os.mkdir(path) Creates a Directory named path os.rmdir(path) Remove (delete) the directory path. Only works when the directory is empty. 3
  • 4. © SkillBrew http://skillbrew.com Working with files and directories (2) Functionality Description os.listdir(path) Return a list containing the names of the entries in the directory given by path os.chdir(path) Change the current working directory to path os.getcwd() Return a string representing the current working directory os.stat(path) Perform the equivalent of a stat() system call on the given path os.symlink(source,li nk_name) Create a symbolic link pointing to source named link_name.(unix only) 4
  • 5. © SkillBrew http://skillbrew.com getcwd() 5 import os curr_dir = os.getcwd() print curr_dir Output: C:UsersPavanPycharmProjectsskillbrew programs returns a string representing the current working directory
  • 6. © SkillBrew http://skillbrew.com Make and change directories 6 import os curr_dir = os.getcwd() print curr_dir os.mkdir(os.path.join(curr_dir, 'foo')) os.chdir(os.path.join(curr_dir, 'foo')) print os.getcwd() Output: C:UsersPavanPycharmProjectsskillbrew programs C:UsersPavanPycharmProjectsskillbrew programsfoo
  • 7. © SkillBrew http://skillbrew.com List all files in directory 7 import os print os.listdir(os.getcwd()) returns a list containing the names of the entries in the directory given by path
  • 8. © SkillBrew http://skillbrew.com os.path – Common pathname manipulations  split  splitext  join 8
  • 9. © SkillBrew http://skillbrew.com split 9 >>> import os >>>os.path.split("/home/user/pictures/foo.jpg") ('/home/user/pictures', 'foo.jpg') >>> filepath, filename = os.path.split( "/home/user/pictures/foo.jpg") >>> filepath '/home/user/pictures' >>> filename 'foo.jpg' split function splits a full pathname and returns a tuple containing the path and filename
  • 10. © SkillBrew http://skillbrew.com splittext 10 >>> import os >>> os.path.splitext('foo.jpg') ('foo', '.jpg') splitext splits a filename and returns a tuple containing the filename and the file extension
  • 11. © SkillBrew http://skillbrew.com join 11 >>> import os >>>filepath, filename = os.path.split( "/home/user/pictures/foo.jpg") >>> print os.path.join(filepath, filename) Output: /home/user/pictures/foo.jpg The join function of os.path constructs a pathname out of one or more partial pathnames
  • 13. © SkillBrew http://skillbrew.com sys module sys module provides a number of functions and variables that can be used to manipulate different parts of the Python runtime environment 13
  • 14. © SkillBrew http://skillbrew.com command-line arguments The argv list contains the arguments passed to the script, when the interpreter was started. The first item contains the name of the script itself. 14 import sys print "script name: %s" % sys.argv[0] print len(sys.argv) $ python sys_ex1.py Output: Script name: sys_ex1.py 1
  • 15. © SkillBrew http://skillbrew.com import sys if len(sys.argv) < 3: print "You have to enter minimum 2 arguments" sys.exit(1) print "The arguments are:" for arg in sys.argv: print arg $ python sys_ex1.py Output: You have to enter minimum 2 arguments command-line arguments (2) 15
  • 16. © SkillBrew http://skillbrew.com import sys if len(sys.argv) < 3: print "You have to enter minimum 2 arguments" sys.exit(1) print "The arguments are:" for arg in sys.argv: print arg $ python sys_ex1.py foo bar Output: The arguments are: sys_ex1.py foo bar command-line arguments (2) 16
  • 17. © SkillBrew http://skillbrew.com Exiting the program  When you reach the end of the main program, the interpreter is automatically terminated  If you need to exit in midflight, you can call the sys.exit function instead  This function takes an optional integer value, which is returned to the calling program  If it is an integer, zero is considered "successful termination" and any nonzero value is considered "abnormal termination" 17
  • 18. © SkillBrew http://skillbrew.com Exiting the program (2) 18 import sys if len(sys.argv) < 3: print "You have to enter minimum 2 arguments" sys.exit(1) print "The arguments are:" for arg in sys.argv: print arg
  • 19. © SkillBrew http://skillbrew.com Summary  os module introduction  Working with files and directories  Make and change directories  List all files in directories  Pathname manipulations  sys module introduction  Command line arguments  Exiting the program 19
  • 20. © SkillBrew http://skillbrew.com References  Sys module details http://effbot.org/librarybook/sys.htm  http://docs.python.org/2/library/sys.html  http://docs.python.org/2/library/os.html#os-file-dir  http://docs.python.org/2/library/os.html 20