Source code for qmmd.qmcalc.admin

import os
from os import listdir, mkdir, rename
from os.path import isdir


[docs] def rmExtension(fileName: str) -> str: """ Remove the extension of a file name. Parameters ---------- fileName : str The name of the file. Returns ------- str The file name without the extension. """ if "." in fileName: splitNames = fileName.split(".") newFileName = "".join(splitNames[:-1]) else: newFileName = fileName return newFileName
[docs] def makeDirForXYZs(inpDirPath: str, verbose: bool = False) -> None: """ Generate new directories with the same name as the existing xyz files. Parameters ---------- inpDirPath : str The directory path containing the xyz files. verbose : bool, optional Whether to display details of the process. """ if verbose: print(f" Making directories for molecules in {inpDirPath}...") xyzFiles = [ f for f in listdir(inpDirPath) if not isdir(f"{inpDirPath}/{f}") and ".xyz" in f ] for xyzFile in xyzFiles: if verbose: print(f" Making directory for {xyzFile}...") mkdir(f"{inpDirPath}/{rmExtension(xyzFile)}") if verbose: print(f" Made directory for {xyzFile}!") if verbose: print(f" DONE -- Made all directories!\n")
[docs] def groupFilesIntoDir(inpDirPath: str, verbose: bool = False) -> None: """ Group files with same names (with extensions removed) into the same directories. Parameters ---------- inpDirPath : str The directory path containing the files to be grouped. verbose : bool, optional Whether to display details of the process. """ if verbose: print(f"\nGrouping molecules in {inpDirPath} into individual directory...") # Make sure a directory is created for each xyz file xyzDirs = [d for d in listdir(inpDirPath) if isdir(f"{inpDirPath}/{d}")] if len(xyzDirs) == 0: makeDirForXYZs(inpDirPath, verbose=verbose) xyzDirs = [d for d in listdir(inpDirPath) if isdir(f"{inpDirPath}/{d}")] # Move each file into the directory with the same name as the file with extension removed if verbose: print(f" Moving molecules in {inpDirPath} into individual directory...") allFiles = [f for f in listdir(inpDirPath) if not isdir(f"{inpDirPath}/{f}")] for f in allFiles: if verbose: print(f" Processing {f}...") filePath = f"{inpDirPath}/{f}" for xyzDir in xyzDirs: if rmExtension(f) == xyzDir: rename(filePath, filePath.replace(f"/{f}", f"/{xyzDir}/{f}")) if verbose: print(f" Moved {f} to {xyzDir}!") if verbose: print(f" DONE -- Moved all files!\n") print(f"DONE -- Grouped all molecules!\n")
if __name__ == "__main__": # Debugging inpDirPath = "/mnt/c/Users/ASUS/Documents/qmmd/src/qmmd/data/exampleXYZs" groupFilesIntoDir(inpDirPath, verbose=True)