Python 003 OS Module example

Source: Internet
Author: User

ImportOS forTmpdirinch('/ tmp'R'c:/users/administrator/pycharmprojects/'):    ifOs.path.isdir (tmpdir): BreakElse:    Print 'No Temp directory available'Tmpdir="'ifTmpdir:os.chdir (tmpdir) CWD=OS.GETCWD ()Print '* * * Current temporary directory'    PrintCWDPrint '* * * Creating example Directory'     whileOs.path.exists ('Example'):         Break    Else: Os.mkdir ('Example') Os.chdir ('Example') CWD=OS.GETCWD ()Print '* * * New working directory'    PrintCWDPrint '* * * Original directory Listing:'    PrintOs.listdir (CWD)Print '* * * Creating test file ...'Fobj= Open ('Test','W') Fobj.write ('foo\n') Fobj.write ('bar\n') Fobj.close ()Print '* * * Updated directory listing:'    PrintOs.listdir (CWD)Print '***renaming "test" fo "filetest.txt"'Os.rename ('Test','Filetest.txt')    Print '***full File Pathname'Path=Os.path.join (Cwd,os.listdir (CWD) [0])PrintPathPrint '* * * (pathname,basename) = ='    Printos.path.split (path)Print '* * * (filename,extension) = ='    PrintOs.path.splitext (Os.path.basename (path))Print '***displaying file Contents:'Fobj=Open (Path) forEachlineinchFobj:Printeachline fobj.close ()Print '***deleting test File'os.remove (path)Print '***updated Directory Listing:'    PrintOs.listdir (CWD) os.chdir (os.pardir)Print '* * * Deleting Test directory'Os.rmdir ('Example')    Print '* * * Done'

Output

C:\Python27\python.exe c:/users/administrator/pycharmprojects/untitled/ospathex.py***Current temporary directoryc:\users\administrator\pycharmprojects***Creating example Directory***New Working Directoryc:\users\administrator\pycharmprojects\example***original directory listing:[]***creating test File ...***Updated directory listing:['Test']Renaming"Test"Fo"Filetest.txt"***Full file Pathnamec:\users\administrator\pycharmprojects\example\filetest.txt(pathname,basename) = =('C:\\users\\administrator\\pycharmprojects\\example','Filetest.txt')(filename,extension) = =('filetest','. txt')***displaying file Contents:foobar***Deleting test file***updated directory listing:[]***Deleting test directoryDone

Python 003 OS Module example

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.