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

os.system(dir)

getcwd()#

It returns the current working directory.

print(os.getcwd())
/home/docs/checkouts/readthedocs.org/user_builds/python-seekho/checkouts/dev/scripts/builtin_modules
print(type(os.getcwd()))
<class 'str'>

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`.
os.path.dirname(p)
'/home/docs/checkouts/readthedocs.org/user_builds/python-seekho/checkouts/dev/scripts'

'/home/docs/checkouts/readthedocs.org/user_builds/python-seekho/checkouts/dev/scripts/builtin_modules'

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

'builtin_modules'

True

remove()#

delete a file from disk

replace()#

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(environ)
print(type(environ))
print(len(environ))
environ({'READTHEDOCS_VIRTUALENV_PATH': '/home/docs/checkouts/readthedocs.org/user_builds/python-seekho/envs/dev', 'READTHEDOCS_CANONICAL_URL': 'https://python-seekho.readthedocs.io/en/dev/', 'HOSTNAME': 'build-26236814-project-717928-python-seekho', 'READTHEDOCS_GIT_CLONE_URL': 'https://github.com/AtrCheema/python-seekho.git', 'HOME': '/home/docs', 'NO_COLOR': '1', 'READTHEDOCS': 'True', 'READTHEDOCS_PRODUCTION_DOMAIN': 'readthedocs.org', 'READTHEDOCS_REPOSITORY_PATH': '/home/docs/checkouts/readthedocs.org/user_builds/python-seekho/checkouts/dev', 'READTHEDOCS_PROJECT': 'python-seekho', 'READTHEDOCS_OUTPUT': '/home/docs/checkouts/readthedocs.org/user_builds/python-seekho/checkouts/dev/_readthedocs/', 'PATH': '/home/docs/checkouts/readthedocs.org/user_builds/python-seekho/envs/dev/bin:/home/docs/.asdf/shims:/home/docs/.asdf/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin', 'READTHEDOCS_VERSION_TYPE': 'branch', 'LANG': 'C.UTF-8', 'READTHEDOCS_LANGUAGE': 'en', 'DEBIAN_FRONTEND': 'noninteractive', 'READTHEDOCS_GIT_COMMIT_HASH': 'ebaff922615a23925da260d6ea0b38c967ed6ee6', 'READTHEDOCS_VERSION_NAME': 'dev', 'READTHEDOCS_VERSION': 'dev', 'PWD': '/home/docs/checkouts/readthedocs.org/user_builds/python-seekho/checkouts/dev/docs/source', 'READTHEDOCS_GIT_IDENTIFIER': 'dev', 'DOCUTILSCONFIG': '/home/docs/checkouts/readthedocs.org/user_builds/python-seekho/checkouts/dev/docs/source/docutils.conf'})
<class 'os._Environ'>
22

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/dev/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/dev/scripts
This new directory has 8 sub-directories/folders in it

walk#

/home/docs/checkouts/readthedocs.org/user_builds/python-seekho/checkouts/dev/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/dev/scripts/builtin_modules/NonExistentFolder ['InsideNonExistentFolder'] []
/home/docs/checkouts/readthedocs.org/user_builds/python-seekho/checkouts/dev/scripts/builtin_modules/NonExistentFolder/InsideNonExistentFolder [] []
/home/docs/checkouts/readthedocs.org/user_builds/python-seekho/checkouts/dev/scripts ['numpy', 'basics', 'advanced', 'oop', 'pandas', 'builtin_modules', 'plotting'] ['readme.txt']
/home/docs/checkouts/readthedocs.org/user_builds/python-seekho/checkouts/dev/scripts/numpy [] ['readme.txt', 'stack_vs_concatenate.py', 'dimensions_axis.py', 'digitize.py']
/home/docs/checkouts/readthedocs.org/user_builds/python-seekho/checkouts/dev/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/dev/scripts/advanced [] ['reg_expressions.py', 'daily_q.csv', 'readme.txt', 'interfacing_with_fortran.py', 'parallel_processing.py', 'interfacing_with_c_cpp.py', 'unit_testing.py', 'read_csv_gpt.cpp', 'interfacing_with_matlab.py', 'interfacing_with_cpp.py', 'speeding_up_with_numba.py', 'scripts_to_executables.py', '_cython.py']
/home/docs/checkouts/readthedocs.org/user_builds/python-seekho/checkouts/dev/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/dev/scripts/pandas [] ['speeding_up.py', 'readme.txt', 'apply.py', 'pivot_vs_melt.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/dev/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/dev/scripts/builtin_modules/NonExistentFolder ['InsideNonExistentFolder'] []
/home/docs/checkouts/readthedocs.org/user_builds/python-seekho/checkouts/dev/scripts/builtin_modules/NonExistentFolder/InsideNonExistentFolder [] []
/home/docs/checkouts/readthedocs.org/user_builds/python-seekho/checkouts/dev/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 we 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', 'speeding_up_with_numba.py', 'scripts_to_executables.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', 'pivot_vs_melt.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 91.

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

Gallery generated by Sphinx-Gallery