Thursday 24 May 2012

Python Utilities : 

We can do a number of things that we do in bash in unix , even in python interpreter .
one thing we have to do in 

import os 

now to see what are all the operations we can do  

EX_CANTCREAT
EX_CONFIG
EX_DATAERR
EX_IOERR
EX_NOHOST
EX_NOINPUT
EX_NOPERM
EX_NOUSER
EX_OK
EX_OSERR
EX_OSFILE
EX_PROTOCOL
EX_SOFTWARE
EX_TEMPFAIL
EX_UNAVAILABLE
EX_USAGE
F_OK
NGROUPS_MAX
O_APPEND
O_ASYNC
O_CREAT
O_DIRECT
O_DIRECTORY
O_DSYNC
O_EXCL
O_LARGEFILE
O_NDELAY
O_NOATIME
O_NOCTTY
O_NOFOLLOW
O_NONBLOCK
O_RDONLY
O_RDWR
O_RSYNC
O_SYNC
O_TRUNC
O_WRONLY
P_NOWAIT
P_NOWAITO
P_WAIT
R_OK
SEEK_CUR
SEEK_END
SEEK_SET
ST_APPEND
ST_MANDLOCK
ST_NOATIME
ST_NODEV
ST_NODIRATIME
ST_NOEXEC
ST_NOSUID
ST_RDONLY
ST_RELATIME
ST_SYNCHRONOUS
ST_WRITE
TMP_MAX
UserDict
WCONTINUED
WCOREDUMP
WEXITSTATUS
WIFCONTINUED
WIFEXITED
WIFSIGNALED
WIFSTOPPED
WNOHANG
WSTOPSIG
WTERMSIG
WUNTRACED
W_OK
X_OK
_Environ
__all__
__builtins__
__doc__
__file__
__name__
__package__
_copy_reg
_execvpe
_exists
_exit
_get_exports_list
_make_stat_result
_make_statvfs_result
_pickle_stat_result
_pickle_statvfs_result
_spawnvef
abort
access
altsep
chdir
chmod
chown
chroot
close
closerange
confstr
confstr_names
ctermid
curdir
defpath
devnull
dup
dup2
environ
errno
error
execl
execle
execlp
execlpe
execv
execve
execvp
execvpe
extsep
fchdir
fchmod
fchown
fdatasync
fdopen
fork
forkpty
fpathconf
fstat
fstatvfs
fsync
ftruncate
getcwd
getcwdu
getegid
getenv
geteuid
getgid
getgroups
getloadavg
getlogin
getpgid
getpgrp
getpid
getppid
getresgid
getresuid
getsid
getuid
initgroups
isatty
kill
killpg
lchown
linesep
link
listdir
lseek
lstat
major
makedev
makedirs
minor
mkdir
mkfifo
mknod
name
nice
open
openpty
pardir
path
pathconf
pathconf_names
pathsep
pipe
popen
popen2
popen3
popen4
putenv
read
readlink
remove
removedirs
rename
renames
rmdir
sep
setegid
seteuid
setgid
setgroups
setpgid
setpgrp
setregid
setresgid
setresuid
setreuid
setsid
setuid
spawnl
spawnle
spawnlp
spawnlpe
spawnv
spawnve
spawnvp
spawnvpe
stat
stat_float_times
stat_result
statvfs
statvfs_result
strerror
symlink
sys
sysconf
sysconf_names
system
tcgetpgrp
tcsetpgrp
tempnam
times
tmpfile
tmpnam
ttyname
umask
uname
unlink
unsetenv
urandom
utime
wait
wait3
wait4
waitpid
walk
write

Now we shall see some examples 

to list contents of dirctory 

os.listdir(directoryname)
list filenames in the directory 

os.path.exists( ) 
to see is the path exists

to do file copy 

import shutil
shutil.copy(source,dest) 

both strings source and dest denote filenames with path 

we cannot do it for copying entire folder content , this can be done by iterating over files inside the folder
Ok these are ways to simulate shell commands in python

Is there a way to run shell commands directly in python and show output 

Ya

import commands 
cmd = "ls -l ."
(status,output) = commands.getstatusoutput(cmd)
print output

this executes ls -l command on directory .  gives the output as a tuple having 2 things ., status (something like exit code, if 0 it means the command executed without error , if command execution had error it exits by sending 1 or even some other error code like 512 ) and the output of the command 


Interesting feature in python :

List comprehension 

if we have a list a = [1, 2 ,3,4]
then you want to get a new list which has the squares of these numbers ..
what we normally do is iterate through the list then get elements and then put it into the new list 

python has a easy way of doing this

b = [num*num      for num in a]

to put additional qualities like removing elements < 2 

b=[num*num     for num in a     if num>2]

No comments:

Post a Comment