Skip to content

Instantly share code, notes, and snippets.

@mikehwang
Created April 20, 2019 04:24
Show Gist options
  • Save mikehwang/351193a8b6fd74fa2c1d9196b9728f09 to your computer and use it in GitHub Desktop.
Save mikehwang/351193a8b6fd74fa2c1d9196b9728f09 to your computer and use it in GitHub Desktop.
Python script to check if all files in one directory exists in another
"""Script checks to see if all files in src exists in dest"""
import os
import subprocess
def does_file_exist(dest: str, name: str):
"""Checks if the file exists
Approach was taken from
https://stackoverflow.com/questions/1724693/find-a-file-in-python
:param dest: Full path of directory to check if file exists
:param name: Name of file to check
:return True if it exists otherwise False
"""
cmd = "find {0} -name \'{1}\' -print -quit".format(dest, name)
lines = [line[2:] for line in subprocess.check_output(cmd, shell=True).splitlines()]
return len(lines) > 0
def check_dir(src: str, dest: str, num=0, num_not_found=0):
"""Check if all files in directory src exists in directory dest
This function is recursive.
:param src: Full path of directory to check if all files exist in dest
:param dest: Full path of directory
:param num: Number of files checked
:param num_not_found: Number of files not found in dest
:return num, num_not_found
"""
for entry in os.scandir(src):
if entry.is_file():
if not does_file_exist(dest, entry.name):
print("Not found: {0}".format(entry.name))
num_not_found += 1
num += 1
if num % 50 == 0:
print("Finished checking {0}".format(num))
else:
num, num_not_found = scan(entry.path, dest, num=num, num_not_found=num_not_found)
return num, num_not_found
if __name__ == "__main__":
src = sys.argv[1]
dest = sys.argv[2]
num, num_not_found = check_dir(src, dest)
print("#checked: {0}, #not found: {1}".format(num, num_not_found))
@mikehwang
Copy link
Author

Do you know how to check directory exist or not with subprocess module. Cuz I don't want to use 2 modules with pretty similar function in program: subprocess and os

@imnotwannafire I think you can use the -type d arg+param for the find command so the find command becomes:

cmd = "find {0} -name -type d \'{1}\' -print -quit".format(dest, name)

@imnotwannafire
Copy link

Do you know how to check directory exist or not with subprocess module. Cuz I don't want to use 2 modules with pretty similar function in program: subprocess and os

@imnotwannafire I think you can use the -type d arg+param for the find command so the find command becomes:

cmd = "find {0} -name -type d \'{1}\' -print -quit".format(dest, name)

Great! Thank you for your help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment