import os from typing import List def scan_dir(path, suffix: str = None): results = [] files = os.listdir(path) for file in files: file_d = os.path.join(path, file) if os.path.isdir(file_d): results.extend(scan_dir(file_d, suffix)) else: if (suffix and file.endswith(suffix)) or (not suffix): results.append(file_d) return results def batch_ln(files: List[str], target: str): cmd = 'ln -s {} {}' for f in files: if os.path.isfile(f): os.system(cmd.format(f, os.path.join(target, os.path.basename(f)))) if __name__ == '__main__': fs = scan_dir('/home/zzh/ocr/pdf', 'pdf') batch_ln(fs, './all_pdf')