Python pathlib.Path.is_file() Examples

The following are 11 code examples of pathlib.Path.is_file(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module pathlib.Path , or try the search function .
Example #1
Source File: script.py    From bulk-downloader-for-reddit with GNU General Public License v3.0 6 votes vote down vote up
def postFromLog(fileName):
    """Analyze a log file and return a list of dictionaries containing
    submissions
    """
    if Path.is_file(Path(fileName)):
        content = JsonFile(fileName).read()
    else:
        print("File not found")
        sys.exit()

    try:
        del content["HEADER"]
    except KeyError:
        pass

    posts = []

    for post in content:
        if not content[post][-1]['TYPE'] == None:
            posts.append(content[post][-1])

    return posts 
Example #2
Source File: selfPost.py    From bulk-downloader-for-reddit with GNU General Public License v3.0 6 votes vote down vote up
def __init__(self,directory,post):

        if "self" in GLOBAL.arguments.skip: raise TypeInSkip

        if not os.path.exists(directory): os.makedirs(directory)

        filename = GLOBAL.config['filename'].format(**post)

        fileDir = directory / (filename+".md")
        print(fileDir)
        print(filename+".md")


        if Path.is_file(fileDir):
            raise FileAlreadyExistsError
            
        try:
            self.writeToFile(fileDir,post)
        except FileNotFoundError:
            fileDir = post['POSTID']+".md"
            fileDir = directory / fileDir

            self.writeToFile(fileDir,post) 
Example #3
Source File: __init__.py    From Mastering-Elasticsearch-7.0 with MIT License 5 votes vote down vote up
def get_py2exe_datafiles():
    data_path = Path(get_data_path())
    d = {}
    for path in filter(Path.is_file, data_path.glob("**/*")):
        (d.setdefault(str(path.parent.relative_to(data_path.parent)), [])
         .append(str(path)))
    return list(d.items()) 
Example #4
Source File: __init__.py    From GraphicDesignPatternByPython with MIT License 5 votes vote down vote up
def get_py2exe_datafiles():
    data_path = Path(get_data_path())
    d = {}
    for path in filter(Path.is_file, data_path.glob("**/*")):
        (d.setdefault(str(path.parent.relative_to(data_path.parent)), [])
         .append(str(path)))
    return list(d.items()) 
Example #5
Source File: font_manager.py    From GraphicDesignPatternByPython with MIT License 5 votes vote down vote up
def list_fonts(directory, extensions):
    """
    Return a list of all fonts matching any of the extensions, found
    recursively under the directory.
    """
    extensions = ["." + ext for ext in extensions]
    return [str(path)
            for path in filter(Path.is_file, Path(directory).glob("**/*.*"))
            if path.suffix in extensions] 
Example #6
Source File: __init__.py    From python3_ios with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def get_py2exe_datafiles():
    data_path = Path(get_data_path())
    d = {}
    for path in filter(Path.is_file, data_path.glob("**/*")):
        (d.setdefault(str(path.parent.relative_to(data_path.parent)), [])
         .append(str(path)))
    return list(d.items()) 
Example #7
Source File: CThermal.py    From Thermal_Image_Analysis with MIT License 5 votes vote down vote up
def get_thermal_image_from_file(thermal_input, thermal_class=CFlir, colormap=None):
    '''
    Function to get the image associated with each RJPG file using the FLIR Thermal base class CFlir
    Saves the thermal images in the same place as the original RJPG
    '''
    CThermal = thermal_class

    inputpath = Path(thermal_input)
    if Path.is_dir(inputpath):
        rjpg_img_paths = list(Path(input_folder).glob('*R.JPG'))
        fff_file_paths = list(Path(input_folder).glob('*.fff'))
        if len(rjpg_img_paths)>0:
            for rjpg_img in tqdm(rjpg_img_paths, total=len(rjpg_img_paths)):
                thermal_obj = CThermal(rjpg_img, color_map=colormap)
                path_wo_ext = str(rjpg_img).replace('_R'+rjpg_img.suffix,'') 
                thermal_obj.save_thermal_image(path_wo_ext+'.jpg')

        elif len(fff_file_paths)>0:
            for fff in tqdm(fff_file_paths, total=len(fff_file_paths)):
                save_image_path = str(fff).replace('.fff','.jpg')
                thermal_obj = CThermal(fff, color_map=colormap)
                thermal_obj.save_thermal_image(save_image_path)
        else:
            logger.error('Input folder contains neither fff or RJPG files')

    elif Path.is_file(inputpath):
        thermal_obj = CThermal(thermal_input, color_map=colormap)
        path_wo_ext = Path.as_posix(inputpath).replace(inputpath.suffix,'')
        thermal_obj.save_thermal_image(path_wo_ext+'.jpg')

    else:
        logger.error('Path given is neither file nor folder. Please check') 
Example #8
Source File: bleu_retrieve.py    From DeepPavlov with Apache License 2.0 5 votes vote down vote up
def load(self, **kwargs) -> None:
        """Load classifier parameters"""
        log.info(f"Loading model from {self.load_path}")
        for path in self.load_path:
            if Path.is_file(path):
                self.ec_data += load_pickle(path)
            else:
                raise FileNotFoundError

        log.info(f"Loaded items {len(self.ec_data)}") 
Example #9
Source File: __init__.py    From coffeegrindsize with MIT License 5 votes vote down vote up
def get_py2exe_datafiles():
    data_path = Path(get_data_path())
    d = {}
    for path in filter(Path.is_file, data_path.glob("**/*")):
        (d.setdefault(str(path.parent.relative_to(data_path.parent)), [])
         .append(str(path)))
    return list(d.items()) 
Example #10
Source File: __init__.py    From CogAlg with MIT License 5 votes vote down vote up
def get_py2exe_datafiles():
    data_path = Path(get_data_path())
    d = {}
    for path in filter(Path.is_file, data_path.glob("**/*")):
        (d.setdefault(str(path.parent.relative_to(data_path.parent)), [])
         .append(str(path)))
    return list(d.items()) 
Example #11
Source File: bench_fio.py    From fio-plot with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def check_target_type(target, filetype):
    """ Validate path and file / directory type.
    It also returns the appropritate fio command line parameter based on the
    file type. 
    """

    keys = ['file', 'device', 'directory']

    test = {
        keys[0]: Path.is_file,
        keys[1]: Path.is_block_device,
        keys[2]: Path.is_dir
    }

    parameter = {
        keys[0]: '--filename',
        keys[1]: '--filename',
        keys[2]: '--directory'
    }

    if not os.path.exists(target):
        print(f"Benchmark target {filetype} {target} does not exist.")
        sys.exit(10)

    if filetype not in keys:
        print(f"Error, filetype {filetype} is an unknown option.")
        exit(123)

    check = test[filetype]

    path_target = Path(target)  # path library needs to operate on path object

    if check(path_target):
        return parameter[filetype]
    else:
        print(f"Target {filetype} {target} is not {filetype}.")
        sys.exit(10)