Reading and Validating Input Data
With a plan in hand, we can consider the inputs our script needs. Input data can come from various sources depending on the task:
-
User input (e.g., a filename or an option entered via keyboard)
-
Files on disk
-
Other systems (like a database or an API)
For our purposes, we’ll focus on reading input either from a user or from files, since our example deals with files.
Reading input means obtaining the necessary data for your script to work on.
In our file-organizing pseudocode, the input is implicitly the list of files in the downloads folder. In other cases, the script might prompt the user for information.
For example, a bulk rename script might ask:
-
“Please enter the directory path:”
-
“Enter the prefix for new filenames:”
These are user inputs that the program needs in order to proceed.
Validating Input
Once input is read, it’s critical to validate it. Input validation is the process of checking that the data provided is sensible, correct, and usable before the main processing happens. This prevents errors or unintended behavior later in the program.
Never trust input blindly – always check it meets your assumptions.
Common Validation Steps
-
Type and Format Check:
Ensure the input is of the expected type or format.
E.g., if you expect a number, verify the input is numeric.
If it’s a file path, ensure it’s a valid path string. -
Presence / Non-empty:
Check that required inputs are not empty or null.
E.g., an empty string where a file name is expected should trigger a prompt again or an error. -
Range or Set Check:
If only certain values are acceptable, verify the input is one of those.
E.g., if the script asks “Type 1 to rename files or 2 to categorize files”, and the user enters 5 — that’s invalid. -
Existence:
For file paths or URLs, check that they actually exist or are reachable.
E.g., if a user provides a directory path, the script should verify that directory exists on disk before proceeding.
How to Validate?
The typical pattern is to use a conditional check and possibly a loop to re-prompt until valid.
Example Pseudocode (Validating a Number Input):
REPEAT
PROMPT "Enter a number from 1 to 10:"
RECEIVE userInput
IF userInput < 1 OR userInput > 10 THEN
PRINT "Invalid input, please try again."
END REPEAT UNTIL userInput >= 1 AND userInput <= 10
This logic keeps asking the user until they enter a valid number within the specified range.
File-Based Input Validation
If the script processes a file (e.g., a CSV), you might:
-
Check that the file can be opened (exists and not locked).
-
Check that the first line has expected headers, or each line has the correct number of columns.
Example Use Case: Organizing Files
Inputs and Validations to Consider:
-
Directory Path (
folder_path):
If this is user-supplied, verify the path is valid and accessible.
Our pseudocode used a fixed “Downloads” path, but a more general script might take this as input.
If invalid, either prompt again or exit with an error message. -
File Types Dictionary:
If user-configurable (e.g., user defines which extensions go to which folder), validate:-
That extensions are correctly formatted (like “.txt”).
-
That target folders exist — or create them if they don’t.
-
-
User Confirmation (Optional):
Show the user what the script will do — especially for destructive actions.
Validate that the user enters “yes”, “no”, “y”, or “n”, and re-prompt if necessary.
Handling Invalid Input
If validation fails, your script should respond gracefully. Options include:
-
Re-prompting the user (e.g., in a loop)
-
Substituting a safe default value (e.g., use
file_if no prefix is given) -
Exiting with a clear error message (if the input is essential and invalid)
The approach depends on the context:
-
For interactive CLI tools, re-prompting is user-friendly.
-
For automated scripts, you might prefer logging the error and exiting.
Example (User Input Path Validation)
Pseudocode:
PROMPT "Enter the path of the folder to organize:"
RECEIVE folder_path
IF folder_path is not a valid directory:
PRINT "Error: The folder path does not exist. Exiting."
END program.
Alternatively, wrap this in a loop to allow retries in case of typos.
Exercise
Think about your task’s inputs:
-
What information does your script need to start working?
-
List the inputs.
-
For each, consider what would make it “invalid.”
Write a short pseudo-check for at least one of them.
Example:
IF file not found OR file extension is not .txt THEN
show error and ask for a correct file.
Doing this will help ensure your script is robust and user-friendly when you implement it.
