Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Python utilities for file handling
file handling using python
################################################################################################
# name: file_count_in_directory.py
# desc:
# date: 2019-03-30
# Author: conquistadorjd
################################################################################################
import os
import glob
print("*** Program Started ***")
dir_name = 'C:/sample/'
file_count = sum([len(files) for r, d, files in os.walk(dir_name)])
print('Number of Files using os.walk :', file_count)
file_count = os.listdir(dir_name)
print("Number of Files using listdir method#1 :", len(file_count))
file_count = len([name for name in os.listdir(dir_name) if os.path.isfile(os.path.join(dir_name, name))])
print("Number of Files using listdir method#2 :", file_count)
file_count = [name for name in os.listdir(dir_name) if os.path.isfile(os.path.join(dir_name, name))]
print("Number of Files using listdir method#3 :", len(file_count))
file_count = glob.glob(dir_name+'*.*')
print("Number of Files using glob :", len(file_count))
print("*** Program Completed ***")
################################################################################################
# name: file_exists_01.py
# desc: Check if file exists
# date: 2019-02-13
# Author: conquistadorjd
################################################################################################
import os
input_file_path = '/home/conquistador/code/github/python-01-utilities/file/'
input_file_name = 'file_exists_01.py'
file_exists = os.path.isfile(input_file_path+input_file_name)
print('file_exists :', file_exists)
input_file_name = 'file_exists_99.py'
file_exists = os.path.isfile(input_file_path+input_file_name)
print('file_exists :', file_exists)
input_file_name = 'file_exists_01.py'
dir_exists = os.path.isdir(input_file_path+input_file_name)
print('dir_exists :', dir_exists)
dir_exists = os.path.isdir(input_file_path)
print('dir_exists :', dir_exists)
input_file_path = '/home/conquistador/code/github/python-01-utilities/fil e/'
dir_exists = os.path.isdir(input_file_path)
print('dir_exists :', dir_exists)
input_file_path = '/home/conquistador/code/github/python-01-utilities/file/'
exists = os.path.exists(input_file_path)
print('exists :', exists)
exists = os.path.exists(input_file_path+input_file_name)
print('exists :', exists)
################################################################################################
# name: file_exists_02.py
# desc: Check if file exists
# date: 2019-02-13
# Author: conquistadorjd
################################################################################################
from pathlib import Path
input_file_path = '/home/conquistador/code/github/python-01-utilities/file/'
input_file_name = 'file_exists_01.py'
var = Path(input_file_path)
print('var : ', var.is_file())
var = Path(input_file_path+input_file_name)
print('var : ', var.is_file())
var = Path(input_file_path)
print('var : ', var.is_dir())
var = Path(input_file_path+input_file_name)
print('var : ', var.is_dir())
var = Path(input_file_path)
print('var : ', var.exists())
var = Path(input_file_path+input_file_name)
print('var : ', var.exists())
################################################################################################
# name: file_size.py
# desc: Getting gile size using python
# date: 2019-02-10
# Author: conquistadorjd
################################################################################################
import io, os
print('*** Program Started ***')
image_path_input = '/home/conquistador/code/github/python-01-utilities/file/input/'
image_name_input = 'file_size-01.jpg' #'file_size-02'
if os.stat(image_path_input + image_name_input).st_size ==0 :
print('Input file is empty')
else :
print('Input file is not empty')
print('File size (in Bytes) : ',os.stat(image_path_input + image_name_input).st_size)
print('File size (in Bytes) : ',os.path.getsize (image_path_input+image_name_input))
image_name_input = 'file_size-02'
if os.stat(image_path_input + image_name_input).st_size ==0 :
print('Input file is empty')
else :
print('Input file is not empty')
print('File size (in Bytes) : ',os.stat(image_path_input + image_name_input).st_size)
print('File size (in Bytes) : ',os.path.getsize (image_path_input+image_name_input))
print('*** Program Ended ***')
################################################################################################
# name: file_with_specific_extension_in_directory.py
# desc:
# date: 2019-03-30
# Author: conquistadorjd
################################################################################################
import os
import glob
print("*** Program Started ***")
dir_name = 'C:/sample/'
matches = []
for root, dirnames, filenames in os.walk(dir_name):
for filename in filenames:
# if filename.endswith(('.txt', '.MOV', '.avi', '.mpg')): ## Checking for multiple extenstions
if filename.endswith('.txt'):
matches.append(filename)
print("List of Files using os.walk:", matches)
matches = []
filenames = os.listdir(dir_name)
for filename in filenames:
# if filename.endswith(('.txt', '.MOV', '.avi', '.mpg')): ## Checking for multiple extenstions
if filename.endswith('.txt'):
matches.append(filename)
print("List of Files using listdir:", matches)
# filenames = glob.glob(dir_name+'*.txt') ## This will return file name with complete path name as well
matches = [os.path.basename(x) for x in glob.glob(dir_name+'*.txt')] ## This will return only file name
print("List of Files using glob :", matches)
print("*** Program Completed ***")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment