-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpathcount.py
More file actions
29 lines (24 loc) · 1.32 KB
/
pathcount.py
File metadata and controls
29 lines (24 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import os # For file/folder operations
import sys # For handling arguments
def pathcount(filename):
current_dir = os.getcwd() # Get the current working directory
full_path = os.path.join(current_dir, filename) # Combine the directory and file name to form the full path
character_count = len(full_path) # Calculate the number of characters in the full path
# Check if the character count exceeds the Windows limit
if character_count >= 255:
raise ValueError(character_count) # Pass the character count to the error handler
return character_count
if __name__ == "__main__":
# Check if a filename is provided as an argument
if len(sys.argv) != 2:
print("Usage: python3 pathcount.py <filename>")
sys.exit(1)
# Get the filename from command-line arguments
filename = sys.argv[1]
try:
character_count = pathcount(filename)
print(f"The number of characters in the file path is: {character_count}")
except ValueError as e: # The file path is too long
character_count = e.args[0] # Receive the character count from the pathcount function
print(f"The length of this file path is {character_count} characters and must be at least {character_count - 255} characters shorter.") # Prints error message
sys.exit(1)