os and os.path Modules : Introduction : File PYTHON TUTORIALS


PYTHON TUTORIALS » File » Introduction »

 

os and os.path Modules


#!/usr/bin/env python

import os

tmpdir = r'c:temp'
if tmpdir:
     os.chdir(tmpdir)
     cwd = os.getcwd()
     print cwd

     os.mkdir('example')
     os.chdir('example')
     cwd = os.getcwd()
     print cwd
     print os.listdir(cwd)

     fobj = open('test', 'w')
     fobj.write('foon')
     fobj.write('barn')
     fobj.close()
     print os.listdir(cwd)

     os.rename('test', 'filetest.txt')
     print os.listdir(cwd)

     path = os.path.join(cwd, os.listdir (cwd)[0])
     print path

     print os.path.split(path)
     print os.path.splitext(os.path.basename(path))

     fobj = open(path)
     for eachLine in fobj:
         print eachLine,
     fobj.close()

     os.remove(path)
     print os.listdir(cwd)
     os.chdir(os.pardir)
     os.rmdir('example')



Leave a Comment / Note


 
Verification is used to prevent unwanted posts (spam). .


PYTHON TUTORIALS

 Navioo File
» Introduction