简体   繁体   中英

Can Python validate if a string represents a file or folder path, and if so, if that file or folder actually exists on-disk?

New to Python and using v3. Trying to find any function that lets me test and validate a passed-in string to see if it represents a file or a folder, and if so, if that file/folder actually exists on disk. I don't need to do anything with that file or folder, just to check if it exists or not for some 'if' logic.

I have recently found the following imports...

import os  
import pathlib

and they look promising, but the examples I've found look like they're actually trying to open/load them, not simply check if they exist, and I haven't found anything to see if the string represents a valid format for a directory or file in the first place.

New to Python and using v3. Trying to find any function that lets me test and validate a passed-in string to see if it represents a file or a folder, and if so, if that file/folder actually exists on disk. I don't need to do anything with that file or folder, just to check if it exists or not for some 'if' logic.

I have recently found the following imports...

import os  
import pathlib

and they look promising, but the examples I've found look like they're actually trying to open/load them, not simply check if they exist, and I haven't found anything to see if the string represents a valid format for a directory or file in the first place.

New to Python and using v3. Trying to find any function that lets me test and validate a passed-in string to see if it represents a file or a folder, and if so, if that file/folder actually exists on disk. I don't need to do anything with that file or folder, just to check if it exists or not for some 'if' logic.

I have recently found the following imports...

import os  
import pathlib

and they look promising, but the examples I've found look like they're actually trying to open/load them, not simply check if they exist, and I haven't found anything to see if the string represents a valid format for a directory or file in the first place.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM