0% found this document useful (0 votes)
7 views23 pages

4_PHP

The document discusses the integration of PHP and Python for web development, comparing their strengths in various aspects such as development environment, language complexity, and security. It details methods for executing Python scripts using PHP and vice versa, highlighting functions like escapeshellcmd(), shell_exec(), and subprocess methods in Python. Additionally, it provides examples of code implementations for executing scripts in both languages, emphasizing the importance of proper command handling and escaping user inputs.

Uploaded by

ridham26kamani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views23 pages

4_PHP

The document discusses the integration of PHP and Python for web development, comparing their strengths in various aspects such as development environment, language complexity, and security. It details methods for executing Python scripts using PHP and vice versa, highlighting functions like escapeshellcmd(), shell_exec(), and subprocess methods in Python. Additionally, it provides examples of code implementations for executing scripts in both languages, emphasizing the importance of proper command handling and escaping user inputs.

Uploaded by

ridham26kamani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

504 – Web Services and Framework (TYBCA Sem-V)

Unit – 4 PHP – Python Integration


PHP and Python are two of the most popular and influential programming
languages. They both have their strengths and weaknesses and share many standard
software development capabilities. Although Rasmus Lerdorf developed PHP as a
web language, and Guido van Rossum created Python as a general-purpose
programming language, technological advances have made both excellent web
development candidates. As with any technology decision, choosing the best one for
your next web development project ultimately comes down to your requirements and
resources. Assessing each language and comparing their strengths across several core
elements can help you make the correct choice.
Development Environment (PHP < Python)
Language Complexity (PHP > Python)
Extendibility (PHP > Python)
Security (PHP > Python)
Scalability and Adaptability (=)
Documentation and Community Support(=)
4.1 Executing Python script using PHP
PHP (which is a recursive acronym for PHP Hypertext Preprocessor) is one of
the most widely used web development technologies in the world. PHP code used for
developing websites and web applications can be embedded directly along with the
rest of the HTML markup. This, along with a rich number of libraries to perform
system tasks, or to integrate with other application, makes PHP the go-to language for
the Internet.
While there are plenty of libraries (or modules) in PHP, there can be cases
when a module for a certain task is either missing or is not properly implemented;
while the same module is available in Python.
Apart from this, for certain scripting tasks the user might find Python more
suitable, while he finds PHP suitable for the rest of his codebase.
4.1.1 Calling Python Script using echo(), escapeshellcmd(), shell_exec()
methods
escapeshellcmd()
escapeshellcmd() escapes any characters in a string that might be used to trick
a shell command into executing arbitrary commands. This function should be used to
make sure that any data coming from user input is escaped before this data is passed
to the exec() or system() functions, or to the backtick operator.
The command line is a dangerous place for unescaped characters. Never pass
unmodified user input to one of PHP's shell-execution functions. Always escape the
appropriate characters in the command and the arguments. This is crucial. It is
unusual to execute command lines that are coming from web forms and not something

SUTEX BANK COLLEGE OF COMPUTER APPLICATIONS & SCIENCE 97


504 – Web Services and Framework (TYBCA Sem-V)

we recommend lightly. However, sometimes you need to run an external program, so


escaping commands and arguments is useful.
Following characters are preceded by a backslash:
#&;`|*?~<>^()[]{}$\, \x0A and \xFF. ' and " are escaped only if they are not paired. In
Windows, all these characters plus % and ! are replaced by a space instead.
Syntax

string escapeshellcmd ( string command )

Parameter
Command- The command that will be escaped.
Return Value
The function/method returns the string after escaping the described chraters
as backtick operators
Example
<?php Output
$command = substr("\\%!** Hello World",0); ^\^%^!^*^* Hello World
$escaped_command = escapeshellcmd($command);
echo ($escaped_command);
?>

shell_exec ()
The shell_exec() function is an inbuilt function in PHP which is used to execute the
commands via shell and return the complete output as a string. The shell_exec is an
alias for the backtick operator, for those used to unix. If the command fails return
NULL and the values are not reliable for error checking.
Syntax:
string shell_exec( $cmd )
Parameters:
This function accepts single parameter $cmd which is used to hold the
command that will be executed.
Return Value:
This function returns the executed command or NULL if an error occurred.
Note:
This function is disabled when PHP is running in safe mode.
Example:
<?php
// shell_exec test
if (shell_exec('mkdir test'))

SUTEX BANK COLLEGE OF COMPUTER APPLICATIONS & SCIENCE 98


504 – Web Services and Framework (TYBCA Sem-V)

echo "test directory created in present working directory";


else
echo "shell_exec failed..!";
//resulting output is found in present working directory if there is no test directory
present
//once the test directory is created on second execution teh command could not
executed
?>
<?php
$output = shell_exec('dir');
echo $output;
?>
Executing Python Script Using PHP
Using above 2 methods along with echo within PHP, one can execute the Python Script
from the web server like Apache. We have to create 2 files for the same purpose. One
file will consist the python script where as the other will consisting of above methods
under the PHP script to execute the mentioned python file.
Check the example below:
Example
test.py Output
#!c:/python310 python Welcome to Python Zone
print("Welcome to Python Zone")
test.php
<?php
$command = escapeshellcmd('python test.py');
$output = shell_exec($command);
echo $output;
?>

Explanation
test.py: in this file the first line mentions from where the python executable file can
locate so that the python code can get execute there after. Whereas the second line is
just a print statement for displaying a string.
test.php: in php file the first line is using the escapeshellcmd function accepts the
command for executing the python file test.py. The second line extract the generated
output of first command in a PHP variable. And third is just used to show the resulted
variable on the browser by just passing the URL of PHP file to execute it.
4.2 Executing PHP Script using Python
4.2.1 Sub-process Module in Python:
The subprocess module present in Python(both 2.x and 3.x) is used to run new
applications or programs through Python code by creating new processes. It also

SUTEX BANK COLLEGE OF COMPUTER APPLICATIONS & SCIENCE 99


504 – Web Services and Framework (TYBCA Sem-V)

helps to obtain the input/output/error pipes as well as the exit codes of various
commands.

A running program is called a process. Each process has its own system state,
which includes memory, lists of open files, a program counter that keeps track of the
instruction being executed, and a call stack used to hold the local variables of
functions.

Normally, a process executes statements one after the other in a single


sequence of control flow, which is sometimes called the main thread of the process. At
any given time, the program is only doing one thing.

A program can create new processes using library functions such as those
found in the os or subprocess modules such as os.fork(), subprocess.Popen(), etc.
However, these processes, known as subprocesses, run as completely independent
entities-each with their own private system state and main thread of execution.

Because a subprocess is independent, it executes concurrently with the original


process. That is, the process that created the subprocess can go on to work on other
things while the subprocess carries out its own work behind the scenes.

Subprocess Module
The subprocess module allows us to:
 spawn new processes
 connect to their input/output/error pipes
 obtain their return codes
4.2.1.1Methods: Check-call(), check-output(), declode(), Popen(),
Communicate(), split()
Popen() function
The Underlying process creation and management in sub-process module is
handled by the popen class. It offers a lot of flexibility so that developers are able to
handle the less common cases not covered by the convenience functions.The P in the
name of the Popen() function stands for process.
If you have multiple instances of an application open, each of those instances is
a separate process of the same program.
For example, if you open multiple windows of your web browser at the same
time, each of those windows is a different process of the web browser program
Syntax:
class subprocess.Popen(args, bufsize=-
1, executable=None, stdin=None, stdout=None, stderr=None, preexec_fn=None, close_fds=True, shell=F
alse, cwd=None, env=None, universal_newlines=None, startupinfo=None, creationflags=0, restore_signa

SUTEX BANK COLLEGE OF COMPUTER APPLICATIONS & SCIENCE 100


504 – Web Services and Framework (TYBCA Sem-V)

ls=True, start_new_session=False, pass_fds=(), *, group=None, extra_groups=None, user=None, umask=


- 1, encoding=None, errors=None, text=None, pipesize=- 1)

But, the general syntax to use subprocess.Popen


subprocess.Popen(cmd,shell=True/False,stdout=subprocess.PIPE,stderr=subprocess.PIPE)

In this syntax we are storing the command output (stdout) and command error
(stderr) in the same variable
Example1:
import subprocess
proc = subprocess.Popen("php demo1.php", shell=True, stdout=subprocess.PIPE)
script_response = proc.stdout.read()
print(script_response)

Let’s assume that demo1.php has 1 line code <?php echo “hello world”; ?>. it will
return hello world in it’s output.
Example:
#!/usr/bin/env python3
import subprocess
# Define command as string
cmd = 'ls -ltr'
# Use shell to execute the command and store it in sp variable
sp = subprocess.Popen(cmd,shell=True)
# Store the return code in rc variable
rc=sp.wait()
# Print the content of sp variable
print(sp)

call() function
Subprocess in Python has a call() method that is used to initiate a program.
The subprocess module provides another function that makes process
spawning a safer operation than using Popen().
The subprocess.call() function waits for the called command/program to finish
reading the output.
Syntax:
subprocess.call(args, *, stdin=None, stdout=None, stderr=None, shell=False, cwd=None, timeout=None,
**other_popen_kwargs)

It supports the same arguments as the Popen() constructor, such as shell,


executable, and cwd, but this time, your script will wait for the program to complete
and populate the return code without the need to communicate().
You can fetch the exit status using Popen.returncode

SUTEX BANK COLLEGE OF COMPUTER APPLICATIONS & SCIENCE 101


504 – Web Services and Framework (TYBCA Sem-V)

To suppress stdout or stderr, supply a value of subprocess. DEVNULL which


indicates that the special file os.devnull will be used.
Example:
import subprocess
# if the script don't need output.
subprocess.call("php demo1.php")

Example:
import subprocess

subprocess.call(["ls", "-l"])

check_call() function
This is another function which is part of subprocess module which can run
command with arguments. check_call will wait for command execution to complete.
check_call() will wait for command execution to complete.
If the execution is successful then the function will return zero then return,
otherwise raise CalledProcessError.
The CalledProcessError object will have the return code in
the returncode attribute.
Syntax:
subprocess.check_call(args, *, stdin=None, stdout=None, stderr=None, shell=False)

Parameters
args: It is the command that you want to execute. You can pass multiple commands by
separating them with a semicolon (;)
stdin: This refers to the standard input stream’s value passed as (os.pipe())
stdout: It is the standard output stream’s obtained value
stderr: This handles any errors that occurred from the standard error stream
shell: It is the boolean parameter that executes the program in a new shell if kept true
Example:
import subprocess
s = subprocess.check_call("gcc HelloWorld.c -o out1;./out1", shell = True)
print(", return code", s)

Note:Do not use stdout=PIPE or stderr=PIPE with call and check_call function as that
can deadlock based on the child process output volume. Use Popen with the
communicate() method when you need pipes.

SUTEX BANK COLLEGE OF COMPUTER APPLICATIONS & SCIENCE 102


504 – Web Services and Framework (TYBCA Sem-V)

check_output() function
The save process output or stdout allows you to store the output of a code
directly in a string with the help of the check_output function.
The subprocess.check_output() is similar to subprocess.run(check=True)
By default, this function will return the data as encoded bytes so you can
use text=True or universal_newlines=True to get string value as output
This function will run command with arguments and return its output.
If the return code was non-zero it raises a CalledProcessError.
The CalledProcessError object will have the return code in
the returncode attribute and any output in the output attribute.
Syntax:
subprocess.check_output(args, *, stdin=None, stderr=None, shell=False, universal_newlines=False
OR
subprocess.check_output(args, *, stdin=None, stderr=None, shell=False, cwd=None, encoding=None, err
ors=None, universal_newlines=None, timeout=None, text=None, **other_popen_kwargs)

Parameters:
args The command to be executed. Several commands can be passed as a string by
separated by “;”.
stdinValue of standard input stream to be passed as pipe(os.pipe()).
stdoutValue of output obtained from standard output stream.
stderrValue of error obtained(if any) from standard error stream.
shellboolean parameter.If True the commands get executed through a new shell
environment.
universal_newlinesBoolean parameter.If true files containing stdout and stderr are
opened in universal newline mode.
Example:
import subprocess
s = subprocess.check_output(["echo", "Hello World!"])
print(s)

Output:
b’Hello World’!

communicate() function
In subprocess,Popen() can interact with the three channels and redirect each
stream to an external file, or to a special value called PIPE. An additional method,
called communicate(), is used to read from the stdout and write on the stdin.
The spawned processes can communicate with the operating system in three
channels:

SUTEX BANK COLLEGE OF COMPUTER APPLICATIONS & SCIENCE 103


504 – Web Services and Framework (TYBCA Sem-V)

 Standard input (stdin)


 Standard output (stdout)
 Standard error (stderr)
The communicate() method can take input from the user and return both the
standard output and the standard error.
Syntax:
Popen.communicate(input=None, timeout=None)
returns a tuple (stdout_data, stderr_data).
Example:
from subprocess import Popen, PIPE
process = Popen(['cat', 'example.py'], stdout=PIPE, stderr=PIPE)
stdout, stderr = process.communicate()
print(stdout)

Output:
b‘‘
… program finished with exit code 0
Press enter to exit console

In the above code, the process.communicate() is the primary call that reads all
the process’s inputs and outputs. The “stdout” handles the process output, and the
“stderr” is for handling any sort of error and is written only if any such error is
thrown.
split() function
Sometime, for example when you use subprocess you might want to run an
external program avoiding the invocation of the shell. For that you need to have the
external command in pieces, not in a single long string.

You could use the regular split method of strings with space as the delimiter,
but that would not give you the right results if some of the pieces contain spaces.

For example if this is the command line:

'./bin/application --source /some/directory --target /other/dir --verbose -d --name


"Foo Bar"'

The solution is to use the split method of the shlex module.In this example we
use both. The str.split() and the shlex.split()

Example:

import shlex

SUTEX BANK COLLEGE OF COMPUTER APPLICATIONS & SCIENCE 104


504 – Web Services and Framework (TYBCA Sem-V)

cmd = './bin/application --source /some/directory --target /other/dir --verbose -d --name "Foo Bar"'
print(cmd.split(' '))
print(shlex.split(cmd))

Output:
['./bin/application', '--source', '/some/directory', '--target', '/other/dir', '--verbose', '-d', '--name',
'"Foo', 'Bar"']
['./bin/application', '--source', '/some/directory', '--target', '/other/dir', '--verbose', '-d', '--name', 'Foo
Bar']

decode() function
There are a couple of methods you can use to convert the byte into string
format for subprocess.Popen output:
 decode("utf-8") (It Return a string decoded from the given bytes. Default
encoding is 'utf-8')
 universal_newlines
In decode, errors may be given to set a different error-handling scheme. The
default for errors is 'strict', meaning that encoding errors raise a UnicodeError. Other
possible values are 'ignore', 'replace' and any other name registered via
codes.register.error().
Syntax:
bytes.decode(encoding='utf-8', errors='strict')
bytearray.decode(encoding='utf-8', errors='strict')

Example:
s = subprocess.check_call("gcc HelloWorld.c -o out1;./out1", shell = True)
print(", return code", s)
print(s.decode("utf-8"))
here, it decode “s” to a normal string

Which subprocess module function should I use?


The functions run(), call(), check_call(), and check_output() are wrappers
around the Popen class.
Using Popen directly gives more control over how the command is run, and
how its input and output streams are processed.
Wait for the command to complete
Use subprocess.call or subprocess.run to run the command described by args.
Wait for command to complete, then return the returncode attribute.
Use subprocess.Popen with wait() to wait for the command to complete
4.2.2 OS Module in Python:
The OS module in Python provides functions for interacting with the operating
system. OS comes under Python’s standard utility modules. This module provides a

SUTEX BANK COLLEGE OF COMPUTER APPLICATIONS & SCIENCE 105


504 – Web Services and Framework (TYBCA Sem-V)

portable way of using operating system-dependent functionality. The *os* and


*os.path* modules include many functions to interact with the file system. It offers
many useful OS functions that are used to perform OS-based tasks and get related
information about operating system.
4.2.2.1write(), read(), close(), mkdir(), makedirs(), path, exists(), isfile(), join()
A file descriptor is small integer value that corresponds to a file or other
input/output resource, such as a pipe or network socket. A File descriptor is an
abstract indicator of a resource and act as handle to perform various lower level I/O
operations like read, write, send etc.
For Example: Standard input is usually file descriptor with value 0, standard
output is usually file descriptor with value 1 and standard error is usually file
descriptor with value 2.
Further files opened by the current process will get the value 3, 4, 5 an so on.
Write()
os.write() method in Python is used to write a byte / string to the given file
descriptor.
Note: os.write() method is intended for low-level operation and should be applied to a
file descriptor as returned by os.open() or os.pipe() method.
Syntax:
os.write(fd, str)
Parameter:
fd: The file descriptor representing the target file.
str: A bytes-like object to be written in the file.
Return Type:
This method returns an integer value which represents the number of bytes
actually written.
Example:
import os
path = "/home / phppython / myworks.txt"
fd = os.open(path, os.O_RDWR)
s = "myworks: A Computer science portal for works."
line = str.encode(s)
numBytes = os.write(fd, line)
print("Number of bytes written:", numBytes)
os.close(fd)

Output:
Number of bytes written: 51

SUTEX BANK COLLEGE OF COMPUTER APPLICATIONS & SCIENCE 106


504 – Web Services and Framework (TYBCA Sem-V)

Read()
os.read() method in Python is used to read at most n bytes from the file
associated with the given file descriptor.
If the end of the file has been reached while reading bytes from the given file
descriptor, os.read() method will return an empty bytes object for all bytes left to be
read.
Note: os.read() method is intended for low-level operation and should be applied to a
file descriptor as returned by os.open() or os.pipe() method.
Syntax:
os.read(fd, n)
Parameter:
fd: A file descriptor representing the file to be read.
n: An integer value denoting the number of bytes to be read from the file associated
with the given file descriptor fd.
Return Type:
This method returns a bytestring which represents the bytes read from the file
associated with the file descriptor fd.
Consider the below text as the content of the file named mywork.txt.
Example:
import os
path = "/home / phppython / myworks.txt"
fd = os.open(path, os.O_RDONLY)
# Number of bytes to be read
n = 51
readBytes = os.read(fd, n)
print(readBytes)
os.close(fd)

Output:
myworks: A Computer science portal for works’

close()
os.close() method in Python is used to close the given file descriptor, so that it
no longer refers to any file or other resource and may be reused.
Syntax:
os.close(fd)
Parameter:
fd: A file descriptor, which is to be closed.
Return Type:
This method does not return any value

SUTEX BANK COLLEGE OF COMPUTER APPLICATIONS & SCIENCE 107


504 – Web Services and Framework (TYBCA Sem-V)

Example:
import os
path = "/ home / phppython / myworks.txt"
fd = os.open(path, os.O_WRONLY)
s = "myworks: A computer science portal for works"
# Convert string to bytes object
line = str.encode(s)
os.write(fd, line)
os.close(fd)
print("File descriptor closed successfully")

Output:
File descriptor closed successfully

os.popen()/write()/read()/close() examples in short


os.popen() function opens a file or from the command specified, and it returns a file
object which is connected to a pipe.
Example
import os
fd = "python.txt"
# popen() is similar to open()
file = open(fd, 'w')
file.write("This is awesome")
file.close()
file = open(fd, 'r')
text = file.read()
print(text)
# popen() provides gateway and accesses the file directly
file = os.popen(fd, 'w')
file.write("This is awesome")
# File not closed, shown in next function.

Output:
This is awesome
os.close()
This function closes the associated file with descriptor fr.
Example
import os
fr = "Python1.txt"
file = open(fr, 'r')
text = file.read()
print(text)
os.close(file)

SUTEX BANK COLLEGE OF COMPUTER APPLICATIONS & SCIENCE 108


504 – Web Services and Framework (TYBCA Sem-V)

Output:
Traceback (most recent call last):

File "main.py", line 3, in

file = open(fr, 'r')

FileNotFoundError: [Errno 2] No such file or directory: 'Python1.txt'

Directory Handling
There are different methods available in the OS module for creating a directory.
These are –
os.mkdir() and os.makedirs()
os.mkdir()
os.mkdir() method in Python is used to create a directory named path with the
specified numeric mode. This method raises FileExistsError if the directory to be
created already exists.
Example:
import os
os.mkdir("d:\\newdir")
It will create the new directory to the path in the string argument of the
function in the D drive.
os.makedirs()
os.makedirs() method in Python is used to create a directory recursively. That
means while making leaf directory if any intermediate-level directory is missing,
os.makedirs() method will create them all.
Example:
#import the os module
from os import listdir, makedirs
#print the list of files and directories in current directory
print("Before adding directory")
print(listdir())
#create a directory
makedirs("./Educative/Leaf", 0o755)
print("After adding directory")
#print the list of files and directories in current directory
print(listdir())
print("Display inner directory")
print(listdir("./Educative"))

output:
Before adding directory
['__ed_script.sh', '__ed_stderr.txt', 'output', '__ed_mem.txt', 'main.py', '__ed_stdout.txt']

SUTEX BANK COLLEGE OF COMPUTER APPLICATIONS & SCIENCE 109


504 – Web Services and Framework (TYBCA Sem-V)

After adding directory


['Educative', '__ed_script.sh', '__ed_stderr.txt', 'output', '__ed_mem.txt', 'main.py', '__ed_stdout.txt']
Display inner directory
['Leaf']

In the code above,


 we import the os module which comes with methods like makedirs mkdir, listdir,
etc.
 we create directories with the names Educative and Leaf in the current
directory ./, and we provide mode as 0o755. This will first create
the Educative directory and then create Leaf inside it.
 In the output, we can check that the directories Educative and Leaf are created.
exists(path)
Test whether a path exists. Returns False for broken symbolic links.
Example:
import os
imaginary = os.path.exists('path/to/file.html')
real = os.path.exists('c:/python/osmodule/main.py')
print(imaginary)
print(real)

output:
False
True

isfile() and isdir()


It Checks to see if the path is a file or if the path is a directory.
Example:
import os
contents = os.listdir()
for item in contents:
if os.path.isdir(item):
print(item + " is a directory")
elif os.path.isfile(item):
print(item + " is a file")

Output:
.idea is a directory
main.py is a file
renamed_file.py is a file

SUTEX BANK COLLEGE OF COMPUTER APPLICATIONS & SCIENCE 110


504 – Web Services and Framework (TYBCA Sem-V)
Sem

os.path.join() :
This method joins various path components with exactly one directory
separator (“/”) following each non
non-empty
empty part except for the last path component. If
the last path component is empty then a directory separator (“/”) is put at the end.
This method returns a string with the concatenated path.
Example1:

Example2:
import os
good_vals = os.environ.get('homedrive')
joined = os.path.join(good_vals, '/index.html')
print(joined)

4.2.2.2isdir(),
(), listdir(), walk(), chdir()
isdir()
OS module in Python provides functions for interacting with the operating
system. OS comes under Python’s standard utility modules. This module provides a
portable way of using operating system dependent functionality. os.path module is
sub module of OS module in Python used for common path name manipulation.
os.path.isdir() method in Python is used to check whether the specified path is
an existing directory or not. This method follows ssymbolic
ymbolic link, that means if the
specified path is a symbolic link pointing to a directory then the method will return
True.
Syntax:
os.path.isdir(path)
Parameter:
path: A path-like
like object representing a file system path.
Return Type: This method returns a Boolean value of class bool. This method
returns True if specified path is an existing directory, otherwise returns False.
Example:
# Python program to explain os.path.isdir() method
# importing os.path module
import os.path
# Path
504 – Web Services and Framework (TYBCA Sem-V)

path = '/home/User/Documents/file.txt'
# Check whether the
# specified path is an
# existing directory or not
isdir = os.path.isdir(path)
print(isdir)
# Path
path = '/home/User/Documents/'
# Check whether the
# specified path is an
# existing directory or not
isdir = os.path.isdir(path)
print(isdir)

Output:
False
True

listdir()
This method in Python is used to get the list of all files and directories in the
specified directory. If we don’t specify any directory, then the list of files and
directories in the current working directory will be returned.
Example:
# Python program to explain os.listdir() method
# importing os module
import os
# Get the list of all files and directories
# in the root directory
path = "/"
dir_list = os.listdir(path)
print("Files and directories in '", path, "' :")
# print the list
print(dir_list)

Output:
Files and directories in ' / ' :
['sys', 'run', 'tmp', 'boot', 'mnt', 'dev', 'proc', 'var', 'bin', 'lib64', 'usr',
'lib', 'srv', 'home', 'etc', 'opt', 'sbin', 'media']

walk()
Suppose we have given below file structure in our system and we want to
traverse all its branches completely from top to bottom?

SUTEX BANK COLLEGE OF COMPUTER APPLICATIONS & SCIENCE 112


504 – Web Services and Framework (TYBCA Sem-V)

How does os.walk() work in python ?


OS.walk() generate the file names in a directory tree by walking the tree either
top-down or bottom-up. For each directory in the tree rooted at directory top
(including top itself), it yields a 3-tuple (dirpath, dirnames, filenames).
root: Prints out directories only from what you specified.
dirs: Prints out sub-directories from root.
files: Prints out all files from root and directories.
Example1:
# Driver function
import os
if __name__ == "__main__":
for (root,dirs,files) in os.walk('Test', topdown=True):
print (root)
print (dirs)
print (files)
print ('--------------------------------')

Example2:
import os
for root, dirs, files in os.walk('c:\python\osmodule'):
for name in files:
print('file: ' + os.path.join(root, name))
for name in dirs:
print('dir: ' + os.path.join(root, name))

output
file: C:\python\osmodule\main.py
file: C:\python\osmodule\renamed_file.py
dir: C:\python\osmodule\.idea
file: C:\python\osmodule\.idea\.gitignore
file: C:\python\osmodule\.idea\misc.xml
file: C:\python\osmodule\.idea\modules.xml
file: C:\python\osmodule\.idea\osmodule.iml
file: C:\python\osmodule\.idea\workspace.xml
504 – Web Services and Framework (TYBCA Sem-V)

dir: C:\python\osmodule\.idea\inspectionProfiles
file: C:\python\osmodule\.idea\inspectionProfiles\profiles_settings.xml
file: C:\python\osmodule\.idea\inspectionProfiles\Project_Default.xml

chdir()
The os module provides the chdir() function to change the current working
directory.
Example:
import os
os.chdir("d:\\")

Output:
d:\\

Path:
The os.path module is a very extensively used module that is handy when
processing files from different places in the system. It is used for different purposes
such as for merging, normalizing and retrieving path names in python . All of these
functions accept either only bytes or only string objects as their parameters. Its results
are specific to the OS on which it is being run.
import os.path vs import os
os.path works strangely actually. It looks like os packaged with a
submodule path but actually, os is a normal module which operate with sys.module to
support os.path. Let us list what happens behind the scenes:
When Python starts, it loads many modules into sys.module.
os module is also loaded when Python starts. It assigns its path to
the os specific module attribute.
It injects sys.modules['os.path'] = path so that you’re able to do import
os.path as though it was a submodule.
os.path module contains some useful functions on pathnames. The path
parameters are either strings or bytes . The result is an object of the same type, if a
path or file name is returned. As there are different versions of operating system so
there are several versions of this module in the standard library.
Following are some functions of OS Path module.
1. os.path.basename(path): This function gives us the last part of the path which
may be a folder or a file name. The difference in how the path is mentioned in
Windows and Linux in terms of the backslash and the forward slash.
Example1:
import os
# In windows
fldr = os.path.basename("C:\Users\xyz\Documents\My Web Sites")
print(fldr)

SUTEX BANK COLLEGE OF COMPUTER APPLICATIONS & SCIENCE 114


504 – Web Services and Framework (TYBCA Sem-V)

file = os.path.basename("C:\Users\xyz\Documents\My Web Sites\intro.html")


print(file)
# In nix*
fldr = os.path.basename("/Documents/MyWebSites")
print(fldr)
file = os.path.basename("/Documents/MyWebSites/music.txt")
print(file)
Running the above code gives us the following result −

Output
My Web Sites
intro.html
MyWebSites

2. os.path.dirname(path) : This function gives us the directory name where the


folder or file is located.
Example
import os
# In windows
DIR = os.path.dirname("C:\Users\xyz\Documents\My Web Sites")
print(DIR)
# In nix*
DIR = os.path.dirname("/Documents/MyWebSites")
print(DIR)
Running the above code gives us the following result −

Output
C:\Users\xyz\Documents
/Documents

3. os.path.isabs(path) : It specifies whether the path is absolute or not. In Unix


system absolute path means path begins with the slash(‘/’) and in Windows that it
begins with a (back)slash after chopping off a potential drive letter.
Example
# isabs function
import os
out = os.path.isabs("/baz/foo")
print(out)

Output:
True

4. os.path.isdir(path): This function specifies whether the path is existing directory


or not.

SUTEX BANK COLLEGE OF COMPUTER APPLICATIONS & SCIENCE 115


504 – Web Services and Framework (TYBCA Sem-V)

Example
# isdir function
import os
out = os.path.isdir("C:\\Users")
print(out)

Output:
True
5. os.path.isfile(path) : Sometimes we may need to check if the complete path given,
represents a folder or a file. If the file does not exist then it will give False as the
output. If the file exists then the output is True.
Example
print(IS_FILE)
IS_FILE = os.path.isfile("C:\Users\xyz\Documents\My Web Sites\intro.html")
print(IS_FILE)
# In nix*
IS_FILE = os.path.isfile("/Documents/MyWebSites")
print(IS_FILE)
IS_FILE = os.path.isfile("/Documents/MyWebSites/music.txt")
print(IS_FILE)

Running the above code gives us the following result −


Output
False
True
False
True
6. os.path.normcase(path): This function normalizes the case of the pathname
specified. In Unix and Mac OS X system it returns the pathname as it is . But in
Windows it converts the path to lowercase and forward slashes to backslashes.
Example
# normcase function in windows
import os
out = os.path.normcase("/BAz")
print(out)

Output:
'\\baz'

7. os.path.normpath(path) : This is an interesting function which will normalize the


given path by eliminating extra slashes or changing the backslash to forward slash

SUTEX BANK COLLEGE OF COMPUTER APPLICATIONS & SCIENCE 116


504 – Web Services and Framework (TYBCA Sem-V)

depending on which OS it is. Which means so that A//B, A/B/, A/./B and A/foo/../B all
become A/B. On Windows, it converts forward slashes to backward slashes.
Example
import os
# Windows path
NORM_PATH = os.path.normpath("C:/Users/Pradeep/Documents/My Web Sites")
print(NORM_PATH)
# Unix Path
NORM_PATH = os.path.normpath("/home/ubuuser//Documents/")
print(NORM_PATH)

Running the above code gives us the following result −


Output
# Running in Windows
C:\Users\Pradeep\Documents\My Web Sites
\home\ubuuser\Documents
# Running in Linux
C:/Users/Pradeep/Documents/My Web Sites
/home/ubuuser/Documents

Example2:
# normpath function in Unix
import os
out = os.path.normpath("foo/./bar")
print(out)

Output:
'foo/bar'
8. os.path.getsize(): In this method, python will give us the size of the file in bytes. To
use this method we need to pass the name of the file as a parameter.
Example
import os #importing os module
size = os.path.getsize("filename")
print("Size of the file is", size," bytes.")

Output:
Size of the file is 192 bytes.

Extra functions:

SUTEX BANK COLLEGE OF COMPUTER APPLICATIONS & SCIENCE 117


504 – Web Services and Framework (TYBCA Sem-V)

split(p)
Split a pathname. Return tuple (head, tail) where tail is everything after the
final slash. Either part may be empty.
import os
split = os.path.split('path/to/file.html')
print(split)

output:
('path/to', 'file.html')

getcwd()
It returns the current working directory(CWD) of the file.
import os
print(os.getcwd())
rmdir()
The rmdir() function removes the specified directory with an absolute or
related path. First, we have to change the current working directory and remove the
folder.
Example
import os
# It will throw a Permission error; that's why we have to change the current working directory.
os.rmdir("d:\\newdir")
os.chdir("..")
os.rmdir("newdir")

rename()
A file or directory can be renamed by using the function os.rename(). A user
can rename the file if it has privilege to change the file.
Example
import os
fd = "python.txt"
os.rename(fd,'Python1.txt')
os.rename(fd,'Python1.txt')

Output:
Traceback (most recent call last):
File "main.py", line 3, in
os.rename(fd,'Python1.txt')
FileNotFoundError: [Errno 2] No such file or directory: 'python.txt' -> 'Python1.txt'

error()

SUTEX BANK COLLEGE OF COMPUTER APPLICATIONS & SCIENCE 118


504 – Web Services and Framework (TYBCA Sem-V)

The os.error() function defines the OS level errors. It raises OSError in case of
invalid or inaccessible file names and path etc.
Example
import os
try:
# If file does not exist,
# then it throw an IOError
filename = 'Python.txt'
f = open(filename, 'rU')
text = f.read()
f.close()
# The Control jumps directly to here if
# any lines throws IOError.
except IOError:
# print(os.error) will <class 'OSError'>
print('Problem reading: ' + filename)

Output:
Problem reading: Python.txt
All functions in the python os module raise the OSError (or subclasses thereof)
when invalid or inaccessible file names and paths, or other arguments that have the
correct type, but are not accepted by the operating system are encountered.

SUTEX BANK COLLEGE OF COMPUTER APPLICATIONS & SCIENCE 119

You might also like