Python Automation and Machine Learning for EM and ICs

An Online Book, Second Edition by Dr. Yougui Liao (2019)

Python Automation and Machine Learning for EM and ICs - An Online Book

Chapter/Index: Introduction | A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | Appendix

Manipulation of File, Folder Names and Create New Folder

This tutorial-style reference shows how to use Python’s built-in os module for automating file and folder operations. It provides examples for creating directories with error handling, conditionally making folders if they don’t already exist, copying and renaming files, listing files by extension, and exporting file names to text. Additional examples cover tasks like bulk renaming, using variables for file naming, and even integrating with screenshot tools. Overall, the page demonstrates practical scripting techniques for streamlining file system management in automation workflows.

The Python os.mkdir() method is used to create a directory with the name path and the provided numeric mode. If the specified directory already exists, then this method throws a FileExistsError.

=========================================================

Create a new folder if the specific folder does not exist. Code:
        Create a new folder and then copy all files from a folder to the new folder and rename the file, and then open the file. If the folder exists, then no file will be copied, but the file will still be opened
Output:        
        Create a new folder and then copy all files from a folder to the new folder and rename the file, and then open the file. If the folder exists, then no file will be copied, but the file will still be opened

=========================================================

List all files and extentions of the files in a folder with "or" to catch different cases. Code:
         Upload Files to Webpages
       Input:    
         Upload Files to Webpages
       Output:    
         Upload Files to Webpages

============================================

Create a new folder if the specific folder does not exist. Code:
        Create a new folder and then copy all files from a folder to the new folder and rename the file, and then open the file. If the folder exists, then no file will be copied, but the file will still be opened
Output:        
        Create a new folder and then copy all files from a folder to the new folder and rename the file, and then open the file. If the folder exists, then no file will be copied, but the file will still be opened

=========================================================

Create file name with variables. Code:
        Create a new folder and then copy all files from a folder to the new folder and rename the file, and then open the file. If the folder exists, then no file will be copied, but the file will still be opened
Output:        
        Create a new folder and then copy all files from a folder to the new folder and rename the file, and then open the file. If the folder exists, then no file will be copied, but the file will still be opened

=========================================================

Create a new folder and then copy all files from a folder to the new folder and rename the file, and then open the file. If the folder exists, then no file will be copied, but the file will still be opened. Code:
        Create a new folder and then copy all files from a folder to the new folder and rename the file, and then open the file. If the folder exists, then no file will be copied, but the file will still be opened
Input:        
        Create a new folder and then copy all files from a folder to the new folder and rename the file, and then open the file. If the folder exists, then no file will be copied, but the file will still be opened        
Output 1:
        Create a new folder and then copy all files from a folder to the new folder and rename the file, and then open the file. If the folder exists, then no file will be copied, but the file will still be opened       
Output 2:
        Create a new folder and then copy all files from a folder to the new folder and rename the file, and then open the file. If the folder exists, then no file will be copied, but the file will still be opened

=========================================================

Screen shot of a defined region on the monitor: code:
         Snipping tool: take a screen shot from the full screen

=========================================================

File name: code:
         Snipping tool: take a screen shot from the full screen

=========================================================

Print and export the folder names and file names (with or without extensions) from a folder into a text file: code:
         Snipping tool: take a screen shot from the full screen
Output:          
         Snipping tool: take a screen shot from the full screen

=========================================================

Rename all files and folders in a directory: code:
         Snipping tool: take a screen shot from the full screen
Input:          
         Snipping tool: take a screen shot from the full screen
Output:          
         Snipping tool: take a screen shot from the full screen
         Snipping tool: take a screen shot from the full screen

 
Topic What it demonstrates Key functions / modules
os.mkdir() description
Create a directory; throws FileExistsError if it exists
os.mkdir
Create folder if not exists
Conditional directory creation
os.path.exists, os.mkdir
List files & extensions with “or”
Enumerate files by multiple extension cases
os.listdir, os.path.splitext, glob
Create folder if not exists (variant)
Alternate implementation
os.path.isdir, os.makedirs
Create file name with variables
Build file names from variables
f-strings, os.path.join
Create → copy → rename → open
Pipeline; skip copy if folder exists
shutil.copy2, os.rename, os.startfile/subprocess
Screenshot defined region
Capture screen area
Windows Snipping Tool via subprocess
File name
Extract file name from path
os.path.basename, pathlib.Path(...).name
Export folder & file names
Write names (with/without extensions) to text
os.listdir, os.path.splitext, open(...,'w')
Bulk rename files & folders
Iterate and rename directory items
os.rename, pathlib.Path.rename
Get all file names in a folder
Using os.listdir() or pathlib
(code) (code)
Extract file name from file path
.name
Extract base name without extension from file path
.stem
Extract extension from file path
.suffix