Skip to main content

The os module in python


This module represents generic operating system functionality. This module is especially important if you want to make your programs platform-independent i.e. it allows the program to be written such that it will run on Linux as well as Windows without any problems and without requiring changes. An example of this is using the os.sep variable instead of the operation system-specific path separator.
Some of the more useful parts of the os module are listed below Most of them are self-explanatory.
  • The os.name string specifies which platform you are using, such as 'nt' for Windows and 'posix' for Linux/Unix users.
  • The os.getcwd() function gets the current working directory i.e. the path of the directory from which the curent Python script is working.
  • The os.getenv() and os.putenv() functions are used to get and set environment variables respectively.
  • The os.listdir() function returns the name of all files and directories in the specified directory.
  • The os.remove() function is used to delete a file.
  • The os.system() function is used to run a shell command.
  • The os.linesep string gives the line terminator used in the current platform. For example, Windows uses '\r\n', Linux uses '\n' and Mac uses '\r'.
  • The os.path.split() function returns the directory name and file name of the path.
        
    >>> os.path.split('/home/swaroop/byte/code/poem.txt')
    ('/home/swaroop/byte/code', 'poem.txt')
        
        
  • The os.path.isfile() and the os.path.isdir() functions check if the given path refers to a file or directory respectively. Similarly, the os.path.exists() function is used to check if a given path actually exists.
You can explore the Python Standard Documentation for more details on these functions and variables. You can use help(sys), etc. as well.

Comments

Popular posts from this blog

HTML Emailing in ZF2

<?php namespace Application\Model; use Zend\Mail\Message; use Zend\Mime\Message as MimeMessage; use Zend\Mime\Part as MimePart; use Zend\Mail\Transport\Sendmail as SendmailTransport; use Zend\Mail\Transport\Smtp as SmtpTransport; use Zend\Mail\Transport\SmtpOptions; use Zend\View\Renderer\PhpRenderer; use Zend\View\Resolver\TemplatePathStack; use Zend\View\Model\ViewModel; use Zend\View\Model\ModelInterface; class ApiEmail extends ApiInterface {    protected $From = "XXXXX@gmail.com";    protected $FromName = "XXXX";    protected $To = "" ;    protected $Subject = "" ;    protected $Body = "" ;    protected $Cc = "XXXXXX@gmail.com";    protected $Bcc = "XXXXX@gmail.com" ;    protected $ReplyTo = "XXXX@gmail.com" ;    protected $Sender = "" ;    protected $Encoding = "UTF-8" ;    protected $MESSAGE ; // getter setter ...

How to setup django | django installation ubuntu

Introduction to Django 1.     High level web framework 1.     Basic modules, classes, and tools to quickly develop and deploy web apps 2.     Contains an ORM (Object-Relational Mapper) that allows for the use of standard Python language syntax when interfacing with a back-end database. 1.     Developer need not learn SQL, DDL, etc! 3.     Provides a template framework that allows HTML, XML, and other “documents” to be converted into “templates”, which can then be converted to “output” via a wide range of substitution techniques. 4.     Elegant URL support (fancy, beautiful URL's, no “?blah=blah”, etc.) 5.     Multi-lingual 2.     Fast and easy to use, robust, flexible, and lots of contributed components available! 1.     Built in administrative interface to manage data models. 2.     Built-in authentication/ac...

Using FileZilla and SFTP with OpenShift