Python os,shutil,subprocess example


You can find an example code for os,shutil,subprocess

#Created on Jul 24, 2013

#@author: tuna

import os
import shutil
import subprocess

print "Current Working Directory", os.getcwd()
print "Getting JAVA_HOME env variable", os.getenv("JAVA_HOME")

#remove a directory with its content and ignore errors =True
shutil.rmtree("/tmp/pythonexample/", True)

#creating a directory with 777 permission
os.mkdir("/tmp/pythonexample/",0777)

#listing directory
direc = os.listdir("/tmp/")

#file name listing inside /tmp
for currfile in direc:
 print currfile

#copy directory tree
shutil.copytree("/home/tuna/Downloads", "/tmp/pythonexample/tmpbackup")

print os.listdir("/tmp/pythonexample/tmpbackup")

#calling xterm as subprocess
subprocess.call("/usr/bin/xterm")

________________________________________________________________________________

output:

Current Working Directory /home/tuna/workspace/hellopython/hellopackage
Getting JAVA_HOME env variable /usr/lib/jvm/java-7-oracle
CRX_75DAF8CB7768
.com.google.Chrome.tCDrFn
ssh-QMAYQPLq1490
keyring-hGEeRu
at-spi2
hsperfdata_tuna
pulse-PKdhtXMmr18n
unity_support_test.0
.ICE-unix
.X0-lock
pulse-j0K7KSQHNQUO
.X11-unix
pythonexample
pulse-2L9K88eMlGn7
[‘google-chrome-stable_current_amd64.deb’]

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s