Python Practice › OS Module Programs
Python OS Module Programs with Solutions
49 programs for the os module and its friends os.path, shutil and subprocess: folders, paths, file counts, copying, backups, permissions, environment variables and running commands. Every solution works inside its own sample folder, so it is safe to run.
Before you start
Read the OS Module and Modules & pip lesson first if the topic is new to you.
All programs
- #1Get the current working directory
Write a Python program to get the current working directory.Easy - #2Read an environment variable
Write a Python program to read an environment variable.Easy - #3Set an environment variable
Write a Python program to set an environment variable for the current process.Easy - #4List environment variables
Write a Python program to get the list of environment variables withos.environ.Easy - #5Create a directory with os.mkdir()
Write a Python program to create a directory withos.mkdir().Easy - #6Create 10 folders with random names
Write a Python program to create 10 directories with random names.Easy - #7Create 10 nested folders
Write a Python program to create 10 directories nested inside each other.Easy - #8Remove an empty folder
Write a Python program to remove an empty directory withos.rmdir().Easy - #9Remove a folder that is not empty
Write a Python program to remove a directory that contains files.Easy - #10Join two paths
Write a Python program to join two paths.Easy - #11Is there a file at a path?
Write a Python program to check whether a file exists at a given path.Easy - #12Is there a folder at a path?
Write a Python program to check whether a directory exists at a given path.Easy - #13List everything in a folder
Write a Python program to list all the files and folders in a target path.Easy - #14Count files in a folder
Write a Python program to count the files in a target path.Easy - #15Count folders in a folder
Write a Python program to count the folders in a target path.Easy - #16Count files per extension
Write a Python program to count the files of each extension in a target path.Easy - #17Copy a file
Write a Python program to copy a file from a source path to a target path.Easy - #18Copy files of one extension
Write a Python program to copy only the files with a specific extension.Easy - #19Copy a file into 10 nested folders
Write a Python program to copy a file into 10 different nested directories.Medium - #20Remove a file
Write a Python program to remove a file at a given path.Easy - #21Remove files of one extension
Write a Python program to remove all files with a specific extension from a folder.Easy - #22Backup sorted by extension
Write a Python backup program that copies every file into a folder named after its extension.Medium - #23Run a system command
Write a Python program to run an operating system command and read its output.Easy - #24Read command-line arguments
Write a Python program that reads arguments passed on the command line.Easy - #25System and Python version
Write a Python program to get operating system and Python version information.Easy - #26Folders searched for programs
Write a Python program to list the folders searched for executables withos.get_exec_path().Easy - #27Random bytes with os.urandom()
Write a Python program to get random bytes withos.urandom()and turn them into a string.Easy - #28Check read permission
Write a Python program to check read and write permission on a path withos.access().Easy - #29File size with os.path.getsize()
Write a Python program to get the size of a file in bytes.Easy - #30Change the working directory
Write a Python program to change the current working directory.Easy - #31List files with os.scandir()
Write a Python program to scan a folder and print all file names withos.scandir().Easy - #32Create a full path with os.makedirs()
Write a Python program to create every folder of a path withos.makedirs().Easy - #33Run another Python script
Write a Python program that runs another Python script.Easy - #34Create files with 10 extensions
Write a Python program to create sample files with 10 different extensions.Easy - #35Print name, path and type
Write a Python program to print the name, path and type (file or folder) of everything in a folder.Easy - #36Print every nested file and folder
Write a Python program to print all nested files and folders withos.walk().Easy - #37Change permissions with os.chmod()
Write a Python program to change a file’s permissions withos.chmod().Easy - #38Change ownership with os.chown()
Write a Python program that changes file ownership withos.chown()where the platform supports it.Easy - #39Rename a folder
Write a Python program to rename a folder withos.rename().Easy - #40Rename with os.renames()
Write a Python program to move a file into new nested folders withos.renames().Easy - #41Change root with os.chroot()
Write a Python program that usesos.chroot()where available.Easy - #42Number of CPUs
Write a Python program to get the number of CPUs withos.cpu_count().Easy - #43Split a path
Write a Python program to split a path into folder and file name.Easy - #44Creation (change) time of a file
Write a Python program to get the ctime of a file withos.path.getctime().Easy - #45Last modified time of a file
Write a Python program to get the last modified time of a file.Easy - #46Does a path exist?
Write a Python program to check whether a path exists.Easy - #47Is a path a symbolic link?
Write a Python program to check whether a path is a symbolic link.Easy - #48File details with os.stat()
Write a Python program to show the details of a file withos.stat().Easy - #49Platform information
Write a Python program to print platform details.Easy