Section 1.
3:
           File System
               I. BEN NASR
   File System
• Identify the platform running a Python    • sys.platform
  script                                    • os: os.getcwd, os.chdir,
• Get the current working directory           os.listdir, os.mkdir,
• Change the current working directory        os.rmdir, os.rename
• List the content of the current working   • os.path: os.path.abspath,
  directory                                   os.path.exists,
• Create and remove directories               os.path.isfile, os.path.isdir
• Rename files and directories
• Recognize the difference between
  relative and absolute paths
• Test whether a file or path exists
Section 1.3
The plateform module
• The platform module in Python is used to access the underlying
  platform’s data, such as, hardware, operating system, and interpreter
  version information.
• You start with importing the platform module in your program, like this:
                            import platform
Platform Functions
 Let's have a look at the different platform functions we can use
 • platform.architecture() returns information about the bit architecture
 • platform.machine() returns the machine type, e.g. 'i386’.
 • platform.node() returns the computer’s network name (may not be fully
   qualified!)
 • platform.platform() returns a single string identifying the underlying platform
 with as much useful information as possible.
 • platform.processor() returns the (real) processor name, e.g. 'amdk6’.
 • platform.python_build() returns a tuple (buildno, builddate) stating the
   Python build number and date as strings.
 • platform.python_compiler() returns a string identifying the compiler used for
   compiling Python.
 • platform.python_version() returns the Python version as string
   'major.minor.patchlevel’
Platform Functions
 •    platform.python_implementation() returns a string identifying the
     Python implementation. Possible return values are: ‘CPython’,
     ‘IronPython’, ‘Jython’, ‘PyPy’.
 •   platform.release() returns the system’s release, e.g. '2.2.0' or 'NT’
 •   platform.system() returns the system/OS name, e.g. 'Linux',
     'Windows', or 'Java’.
 •   platform.version() returns the system’s release version, e.g. '#3 on
     degas’
 •   platform.uname() returns a tuple of strings (system, node, release,
     version, machine, processor) identifying the underlying platform.
 Example
import platform
print ('uname:', platform.uname())
uname_result(system='Windows', node='DESKTOP-IM03TR9', release='10', version='10.0.18362', machine='AMD64',
processor='Intel64 Family 6 Model 61 Stepping 4, GenuineIntel’)
print ('system :', platform.system() )
system: Windows
Print('node :', platform.node() )
DESKTOP-IM03TR9
print ('release :’, platform.release())
10
Print ( 'version :’, platform.version())
'10.0.18362'
 print 'machine :', platform.machine()
AMD64
print 'processor:', platform.processor()
'Intel64 Family 6 Model 61 Stepping 4, GenuineIntel'
Python Directory
 • how to handle with Python directory.
     ocreate, rename, list files in a directory in Python,
      and work with the Python Directory.
 • In a computer system, files are organized into directories. These may
   contain subdirectories and files.
 • Indeed, this makes a vital part of a user-friendly UI.
 • In this Python Directory tutorial, we will import the OS module to be
   able to access the methods we will apply.
• >>> import os
Getting Current Python Directory
• To find out which directory in python you are currently in, use the
  getcwd() method.
>>> os.getcwd()
 'C:\\Users\\ben nasr\\AppData\\Local\\Programs\\Python\\Python37’
>>>print(os.getcwd())
• cwd is for current working directory in python. This returns the path of
  the current python directory as a string in Python.
Changing Current Python Directory
• To change our current working directories in python, we use the
  chdir() method. This takes one argument- the path to the directory to
  which to change.
>>> os.chdir('C:\Users\lifei')
Python List Directories and Files
1) To get the contents of a directory into a python list, we use the listdir() method.
     >>> os.listdir()  ['DLLs', 'Doc', 'include', 'Lib', 'libs', 'LICENSE.txt', 'NEWS.txt', 'python.exe',
         'python3.dll', 'python37.dll', 'pythonw.exe', 'Scripts', 'tcl', 'Tools', 'vcruntime140.dll']
2) How to Create Python Directory?
• We can also create new python directories with the mkdir() method.
         >>> os.mkdir('Photos')
         >>> os.listdir()  ['DLLs', 'Doc', 'include', 'Lib', 'libs', 'LICENSE.txt', 'NEWS.txt', 'Photos',
         'python.exe', 'python3.dll', 'python37.dll', 'pythonw.exe', 'Scripts', 'tcl', 'Tools', 'vcruntime140.dll’]
3) How to Rename Python Directory?
• To rename directories in python, we use the rename() method. It takes two
  arguments- the python directory to rename, and the new name for it.
         >>> os.rename('Photos’,’Photos 2020’)
         >>> os.listdir()  ['DLLs', 'Doc', 'include', 'Lib', 'libs', 'LICENSE.txt', 'NEWS.txt', 'Photos
2020', 'python.exe', 'python3.dll', 'python37.dll', 'pythonw.exe', 'Scripts', 'tcl', 'Tools', 'vcruntime140.dll']
Python List Directories and Files
 4) How to Remove Python Directory/File?
 >>> os.rmdir('Photos 2020')
 >>> os.rmdir('Photos 2020')
 Traceback (most recent call last):
 File "<pyshell#412>", line 1, in <module>
os.path
 1) os.path.exists()
 Using path.exists you can quickly check that a file or directory exists.
 Steps 1) Before you run the code, it is important that you import the os.path
 module.
 >>> import os.path
 >>> from os import path
 Steps 2) Now, use the path.exists() function to check whether a File Exists.
 A file
 >>> path.exists('C:\\Users\\ben nasr\\Desktop\\python\\F1.txt’)
 true
 A directory
 >>> os.path.exists('C:\\Users\\ben nasr\\Desktop\\python’)
 true
os.path
2) os.path.isfile()
We can use the isfile command to check whether a given input is a file or directory.
>>>print ("Is it File?" + str(path.isfile('guru99.txt’)))
>>>print ("Is it File?" +
str(os.path.isfile('C:\\Users\\bennasr\\Desktop\\python\\F1.txt')))
 >>>print ("Is it File?" + str(path.isfile('myDirectory')))
os.path
 3) os.path.isdir()
 If we want to confirm that a given path points to a directory, we can use the os.path.dir()
 function
 >>>print ("Is it Directory?" + str(path.isdir('guru99.txt')))
 >>>print ("Is it Directory?" + str(path.isdir('myDirectory')))
os.path
                                   • Parameter:
                                     Path: A path-like object representing
          3) abspath(path)           a file system path.
                                   • Return Type: This method returns a
                                     normalized version of the pathname
                                     path.
 • Syntax: os.path.abspath(path)
Section
Example 1
# Python program to demonstrate
# os.path.abspath()
import os.path
# file name
file_name = 'GFG.txt'
# prints the absolute path of current
# working directory with file name
print(os.path.abspath(file_name))
Example 2
This function can also return the normalized path after changing the
current working directory.
# os.path.abspath()
import os
# file name
file_name = 'GFG.txt'
# change the current working directory
os.chdir("/home/geeks/")
# prints the absolute path of current
# working directory with file name
print(os.path.abspath(file_name))