2.1 os#

Important

This lesson is still under development.

When python is installed, several modules/packages are pre-installed in it. These modules provide some very basic functionality to the user. The os is such a built-in module which provides the functionalities about paths or operating system.

import os

system()#

It is used to execute commands that we would otherwise execute using command prompt on windows or on terminal. For example on Windows we can do os.system(dir) where dir is a command for Windows command prompt

getcwd()#

It returns the current working directory.

print(os.getcwd())
/home/docs/checkouts/readthedocs.org/user_builds/python-seekho/checkouts/latest/scripts/builtin_modules

path#

The path submodule of os module contains several helpful functions to manipulate path.

Find out whether the path p exists or not

True

Find out whther p is a file or not

False

Find the parent directory of p.

'/home/docs/checkouts/readthedocs.org/user_builds/python-seekho/checkouts/latest/scripts'
'/home/docs/checkouts/readthedocs.org/user_builds/python-seekho/checkouts/latest/scripts/builtin_modules'
('/home/docs/checkouts/readthedocs.org/user_builds/python-seekho/checkouts/latest/scripts', 'builtin_modules')
'builtin_modules'
True

remove()#

delete a file from disk

replace()#

open()#

write()#

close()#

listdir#

Returns a list of folders/directories in a given path.

['readme.txt', '_os.py', '_types_typing.py', '_csv.py', '_warnings.py', '_random_module.py', '_time.py', '_pathlib.py', '_sys.py', '_copy.py', '_collections.py', '_itertools.py', '_math.py']

environ#

environ = os.environ
print(type(environ))
print(len(environ))
<class 'os._Environ'>
20

wait()#

rename()#

renames()#

mkdir()#

creates a directory if all its upper/parent directories are present.

False
True
p = os.path.join(os.getcwd(), "NonExistentFolder", "InsideNonExistentFolder")

print(os.path.exists(p))
False

uncomment following line

# os.mkdir(p)  # FileNotFoundError

os.makedirs#

True

rmdir()#

True
False

cpu_count()#

Cound the number of cpus/cores/processes available.

print(os.cpu_count())
2

chdir()#

original_wd = os.getcwd()
print("Current working directory is \n", original_wd)
print(f"It has {len(os.listdir(os.getcwd()))} sub-directories/folders in it.")
new_wd = os.path.join(os.path.dirname(original_wd))
os.chdir(new_wd)
print("Now we have changed working directory to \n", new_wd)
print(f"This new directory has {len(os.listdir(os.getcwd()))} sub-directories/folders in it")
# now changing back to original
os.chdir(original_wd)
Current working directory is
 /home/docs/checkouts/readthedocs.org/user_builds/python-seekho/checkouts/latest/scripts/builtin_modules
It has 14 sub-directories/folders in it.
Now we have changed working directory to
 /home/docs/checkouts/readthedocs.org/user_builds/python-seekho/checkouts/latest/scripts
This new directory has 8 sub-directories/folders in it

walk#

/home/docs/checkouts/readthedocs.org/user_builds/python-seekho/checkouts/latest/scripts/builtin_modules ['NonExistentFolder'] ['readme.txt', '_os.py', '_types_typing.py', '_csv.py', '_warnings.py', '_random_module.py', '_time.py', '_pathlib.py', '_sys.py', '_copy.py', '_collections.py', '_itertools.py', '_math.py']
/home/docs/checkouts/readthedocs.org/user_builds/python-seekho/checkouts/latest/scripts/builtin_modules/NonExistentFolder ['InsideNonExistentFolder'] []
/home/docs/checkouts/readthedocs.org/user_builds/python-seekho/checkouts/latest/scripts/builtin_modules/NonExistentFolder/InsideNonExistentFolder [] []
/home/docs/checkouts/readthedocs.org/user_builds/python-seekho/checkouts/latest/scripts ['numpy', 'basics', 'advanced', 'oop', 'pandas', 'builtin_modules', 'plotting'] ['readme.txt']
/home/docs/checkouts/readthedocs.org/user_builds/python-seekho/checkouts/latest/scripts/numpy [] ['readme.txt', 'stack_vs_concatenate.py', 'dimensions_axis.py', 'digitize.py']
/home/docs/checkouts/readthedocs.org/user_builds/python-seekho/checkouts/latest/scripts/basics [] ['nested_functions.py', 'for_loops.py', 'installing_packages.py', 'conditional_statements.py', 'copying_lists.py', 'args_and_kwargs.py', 'readme.txt', 'exceptions.py', 'lists.py', 'while_loops.py', 'dictionaries.py', 'generators.py', 'NewFile.json', 'functions.py', 'iterator_vs_iterable.py', 'variables.py', 'operators.py', 'sets.py', 'io_operations.py', 'global_vs_local.py', 'sequential_data.py', 'NewFile', 'keywords.py', 'print_function.py']
/home/docs/checkouts/readthedocs.org/user_builds/python-seekho/checkouts/latest/scripts/advanced [] ['reg_expressions.py', 'readme.txt', 'interfacing_with_fortran.py', 'parallel_processing.py', 'interfacing_with_c_cpp.py', 'unit_testing.py', 'interfacing_with_matlab.py', 'interfacing_with_cpp.py', '_cython.py']
/home/docs/checkouts/readthedocs.org/user_builds/python-seekho/checkouts/latest/scripts/oop [] ['class.py', '__getattr__.py', 'readme.txt', 'init.py', 'inheritance.py', 'call.py', 'class_vs_instance_attributes.py', 'attributes.py', 'str_repr.py', 'class_methods.py', 'introduction.py', 'public_vs_private_attributes.py', 'magical_methods.py', 'methods.py', 'static_methods.py', 'getattr_setattr.py', 'descriptors.py', 'property.py']
/home/docs/checkouts/readthedocs.org/user_builds/python-seekho/checkouts/latest/scripts/pandas [] ['speeding_up.py', 'readme.txt', 'apply.py', 'multi-indexing.py', 'groupby.py', 'working_with_timeseries.py', 'dataframe_vs_series.py', 'io.py', 'indexing_vs_slicing.py']
/home/docs/checkouts/readthedocs.org/user_builds/python-seekho/checkouts/latest/scripts/builtin_modules ['NonExistentFolder'] ['readme.txt', '_os.py', '_types_typing.py', '_csv.py', '_warnings.py', '_random_module.py', '_time.py', '_pathlib.py', '_sys.py', '_copy.py', '_collections.py', '_itertools.py', '_math.py']
/home/docs/checkouts/readthedocs.org/user_builds/python-seekho/checkouts/latest/scripts/builtin_modules/NonExistentFolder ['InsideNonExistentFolder'] []
/home/docs/checkouts/readthedocs.org/user_builds/python-seekho/checkouts/latest/scripts/builtin_modules/NonExistentFolder/InsideNonExistentFolder [] []
/home/docs/checkouts/readthedocs.org/user_builds/python-seekho/checkouts/latest/scripts/plotting [] ['_bar.py', 'readme.txt', '_plot.py', '_contour.py', 'marginal_plots.py', '_scatter.py', '_circular_bar.py', '_parallel_plot.py', 'intro.py', '_hist.py', '_dumbell.py', '_regplot.py', '_lollipop.py', '_ridge.py', '_boxplot.py', 'pie.py', '_taylor.py', '_violin.py', '_imshow.py', '_spider.py']

finding files#

Let’s create few files first.

for i in range(5):
    with open(f'TextFile_{i}.txt', 'w'):
        pass
for i in range(5):
    with open(f'CsvFile_{i}.csv', 'w'):
        pass

If we want to find all files and folders within a specific path

['NonExistentFolder', 'TextFile_1.txt', 'CsvFile_0.csv', 'readme.txt', 'TextFile_4.txt', '_os.py', 'CsvFile_4.csv', '_types_typing.py', 'CsvFile_1.csv', '_csv.py', 'CsvFile_2.csv', '_warnings.py', 'TextFile_2.txt', '_random_module.py', '_time.py', '_pathlib.py', '_sys.py', 'CsvFile_3.csv', 'TextFile_0.txt', '_copy.py', '_collections.py', '_itertools.py', '_math.py', 'TextFile_3.txt']

If want to find only files and not folders/directories, we can do following list comprehension.

print([f for f in os.listdir(path_to_look) if os.path.isfile(f)])
['TextFile_1.txt', 'CsvFile_0.csv', 'readme.txt', 'TextFile_4.txt', '_os.py', 'CsvFile_4.csv', '_types_typing.py', 'CsvFile_1.csv', '_csv.py', 'CsvFile_2.csv', '_warnings.py', 'TextFile_2.txt', '_random_module.py', '_time.py', '_pathlib.py', '_sys.py', 'CsvFile_3.csv', 'TextFile_0.txt', '_copy.py', '_collections.py', '_itertools.py', '_math.py', 'TextFile_3.txt']

If we want to find files with a specific extension, we can do as following

print([f for f in os.listdir(path_to_look) if os.path.isfile(f) and f.endswith(".txt")])
['TextFile_1.txt', 'readme.txt', 'TextFile_4.txt', 'TextFile_2.txt', 'TextFile_0.txt', 'TextFile_3.txt']

If we want to find files with a specific extension, and starting with a specific name, we can do as following

print([f for f in os.listdir(path_to_look) if os.path.isfile(f) and f.endswith(".txt") and f.startswith('TextFile_')])
['TextFile_1.txt', 'TextFile_4.txt', 'TextFile_2.txt', 'TextFile_0.txt', 'TextFile_3.txt']

number of lessons in python-seekho book

scripts = [fname  for f in os.walk(python_seekho_scripts) for fname in f[2] if fname.endswith('.py')]
print(scripts)
['stack_vs_concatenate.py', 'dimensions_axis.py', 'digitize.py', 'nested_functions.py', 'for_loops.py', 'installing_packages.py', 'conditional_statements.py', 'copying_lists.py', 'args_and_kwargs.py', 'exceptions.py', 'lists.py', 'while_loops.py', 'dictionaries.py', 'generators.py', 'functions.py', 'iterator_vs_iterable.py', 'variables.py', 'operators.py', 'sets.py', 'io_operations.py', 'global_vs_local.py', 'sequential_data.py', 'keywords.py', 'print_function.py', 'reg_expressions.py', 'interfacing_with_fortran.py', 'parallel_processing.py', 'interfacing_with_c_cpp.py', 'unit_testing.py', 'interfacing_with_matlab.py', 'interfacing_with_cpp.py', '_cython.py', 'class.py', '__getattr__.py', 'init.py', 'inheritance.py', 'call.py', 'class_vs_instance_attributes.py', 'attributes.py', 'str_repr.py', 'class_methods.py', 'introduction.py', 'public_vs_private_attributes.py', 'magical_methods.py', 'methods.py', 'static_methods.py', 'getattr_setattr.py', 'descriptors.py', 'property.py', 'speeding_up.py', 'apply.py', 'multi-indexing.py', 'groupby.py', 'working_with_timeseries.py', 'dataframe_vs_series.py', 'io.py', 'indexing_vs_slicing.py', '_os.py', '_types_typing.py', '_csv.py', '_warnings.py', '_random_module.py', '_time.py', '_pathlib.py', '_sys.py', '_copy.py', '_collections.py', '_itertools.py', '_math.py', '_bar.py', '_plot.py', '_contour.py', 'marginal_plots.py', '_scatter.py', '_circular_bar.py', '_parallel_plot.py', 'intro.py', '_hist.py', '_dumbell.py', '_regplot.py', '_lollipop.py', '_ridge.py', '_boxplot.py', 'pie.py', '_taylor.py', '_violin.py', '_imshow.py', '_spider.py']
print(f"Total number of lessons in python-seekho book are {len(scripts)}.")
Total number of lessons in python-seekho book are 88.

Total running time of the script: ( 0 minutes 0.012 seconds)

Gallery generated by Sphinx-Gallery