utils.files.file
Common file operations.
1""" 2Common file operations. 3""" 4 5import pathlib 6 7from ..logs.log import logger 8 9from typing import Union, Optional 10 11 12def directoryExists( 13 directoryCandidate: Union[str, pathlib.Path] 14) -> Optional[pathlib.Path]: 15 """ 16 Check whether provided path exists and is a directory. 17 18 Example: 19 20 ``` py 21 from phab.utils.files import file as fl 22 23 someDirectoryPath: str = "/path/to/some/directory/" 24 someDirectory = fl.directoryExists(someDirectoryPath) 25 if someDirectory is None: 26 raise ValueError( 27 f"Provided path to [{someDirectoryPath}] seems to be wrong" 28 ) 29 else: 30 # now you can do something with that directory 31 ``` 32 """ 33 directoryPath: pathlib.Path = pathlib.Path() 34 35 if isinstance(directoryCandidate, str): 36 directoryPath = pathlib.Path(directoryCandidate) 37 elif isinstance(directoryCandidate, pathlib.Path): 38 directoryPath = directoryCandidate 39 else: 40 msg = " ".join(( 41 "Unsupported type of the directory path:", 42 type(directoryCandidate) 43 )) 44 logger.error(msg) 45 # raise ValueError(msg) 46 return None 47 48 if not directoryPath.exists(): 49 msg = f"The path [{directoryPath}] does not exist" 50 logger.error(msg) 51 # raise ValueError(msg) 52 return None 53 54 if not directoryPath.is_dir(): 55 msg = f"The path [{directoryPath}] is not a directory" 56 logger.error(msg) 57 # raise ValueError(msg) 58 return None 59 60 return directoryPath 61 62 63def fileExists( 64 fileCandidate: Union[str, pathlib.Path] 65) -> Optional[pathlib.Path]: 66 """ 67 Check whether provided path exists and is a file. 68 69 Example: 70 71 ``` py 72 from phab.utils.files import file as fl 73 74 someFilePath: str = "/path/to/some.file" 75 someFile = fl.fileExists(someFilePath) 76 if someFile is None: 77 raise ValueError( 78 f"Provided path to [{someFilePath}] seems to be wrong" 79 ) 80 else: 81 # now you can do something with that file 82 ``` 83 """ 84 filePath: pathlib.Path = pathlib.Path() 85 86 if isinstance(fileCandidate, str): 87 filePath = pathlib.Path(fileCandidate) 88 elif isinstance(fileCandidate, pathlib.Path): 89 filePath = fileCandidate 90 else: 91 msg = " ".join(( 92 "Unsupported type of the file path:", 93 type(fileCandidate) 94 )) 95 logger.error(msg) 96 # raise ValueError(msg) 97 return None 98 99 if not filePath.exists(): 100 msg = f"The path [{filePath}] does not exist" 101 logger.error(msg) 102 # raise ValueError(msg) 103 return None 104 105 if not filePath.is_file(): 106 msg = f"The path [{filePath}] is not a file" 107 logger.error(msg) 108 # raise ValueError(msg) 109 return None 110 111 return filePath
def
directoryExists(directoryCandidate: Union[str, pathlib.Path]) -> Optional[pathlib.Path]:
13def directoryExists( 14 directoryCandidate: Union[str, pathlib.Path] 15) -> Optional[pathlib.Path]: 16 """ 17 Check whether provided path exists and is a directory. 18 19 Example: 20 21 ``` py 22 from phab.utils.files import file as fl 23 24 someDirectoryPath: str = "/path/to/some/directory/" 25 someDirectory = fl.directoryExists(someDirectoryPath) 26 if someDirectory is None: 27 raise ValueError( 28 f"Provided path to [{someDirectoryPath}] seems to be wrong" 29 ) 30 else: 31 # now you can do something with that directory 32 ``` 33 """ 34 directoryPath: pathlib.Path = pathlib.Path() 35 36 if isinstance(directoryCandidate, str): 37 directoryPath = pathlib.Path(directoryCandidate) 38 elif isinstance(directoryCandidate, pathlib.Path): 39 directoryPath = directoryCandidate 40 else: 41 msg = " ".join(( 42 "Unsupported type of the directory path:", 43 type(directoryCandidate) 44 )) 45 logger.error(msg) 46 # raise ValueError(msg) 47 return None 48 49 if not directoryPath.exists(): 50 msg = f"The path [{directoryPath}] does not exist" 51 logger.error(msg) 52 # raise ValueError(msg) 53 return None 54 55 if not directoryPath.is_dir(): 56 msg = f"The path [{directoryPath}] is not a directory" 57 logger.error(msg) 58 # raise ValueError(msg) 59 return None 60 61 return directoryPath
Check whether provided path exists and is a directory.
Example:
from phab.utils.files import file as fl
someDirectoryPath: str = "/path/to/some/directory/"
someDirectory = fl.directoryExists(someDirectoryPath)
if someDirectory is None:
raise ValueError(
f"Provided path to [{someDirectoryPath}] seems to be wrong"
)
else:
# now you can do something with that directory
def
fileExists(fileCandidate: Union[str, pathlib.Path]) -> Optional[pathlib.Path]:
64def fileExists( 65 fileCandidate: Union[str, pathlib.Path] 66) -> Optional[pathlib.Path]: 67 """ 68 Check whether provided path exists and is a file. 69 70 Example: 71 72 ``` py 73 from phab.utils.files import file as fl 74 75 someFilePath: str = "/path/to/some.file" 76 someFile = fl.fileExists(someFilePath) 77 if someFile is None: 78 raise ValueError( 79 f"Provided path to [{someFilePath}] seems to be wrong" 80 ) 81 else: 82 # now you can do something with that file 83 ``` 84 """ 85 filePath: pathlib.Path = pathlib.Path() 86 87 if isinstance(fileCandidate, str): 88 filePath = pathlib.Path(fileCandidate) 89 elif isinstance(fileCandidate, pathlib.Path): 90 filePath = fileCandidate 91 else: 92 msg = " ".join(( 93 "Unsupported type of the file path:", 94 type(fileCandidate) 95 )) 96 logger.error(msg) 97 # raise ValueError(msg) 98 return None 99 100 if not filePath.exists(): 101 msg = f"The path [{filePath}] does not exist" 102 logger.error(msg) 103 # raise ValueError(msg) 104 return None 105 106 if not filePath.is_file(): 107 msg = f"The path [{filePath}] is not a file" 108 logger.error(msg) 109 # raise ValueError(msg) 110 return None 111 112 return filePath
Check whether provided path exists and is a file.
Example:
from phab.utils.files import file as fl
someFilePath: str = "/path/to/someutils.files.file"
someFile = fl.fileExists(someFilePath)
if someFile is None:
raise ValueError(
f"Provided path to [{someFilePath}] seems to be wrong"
)
else:
# now you can do something with that file