'python - check if path is valid for multiple paths
I am having struggle with size of my code. We are making program for sorting old data based on its name.
So I made paths declarations:
pdf_source = pathlib.Path(r"C:\PDFS_SOURCE")
pdf_destination_contract = pathlib.Path(r"...")
pdf_destination_order = pathlib.Path(r"...")
In fact paths are very long and hard to read and fix, when something changes.
and like this it goes for dozen of categories and multiple file types.
Is there way to check for all of these paths if directory is valid and exists?
In meaning to avoid doing condition .isdir() for each inserted path.
P.S.: Reason why we do it this way is because we need to sort files for users into directories and they could be changed/rename.
Thanks to Barmar - here is solution:
import os
a = "./DIR_FILES"
b = "./DOC_FILES"
c = "./OH_SNAP"
path_list = [a, b ,c]
def valid_path(input_path):
if os.path.isdir(input_path):
print(input_path , " exists")
else:
print(input_path , " not exist")
for path in path_list:
valid_path(path)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
