Jan-06-2020, 03:42 AM
The following code will return a path where to place file, based of file name.
will work with all file suffixes as written, searches filename stem for aircraft type.
Automatically creates directory if it doesn't already exist.
Won't harm existing directories.
change homepath to your base directory, for your code should, this should be (I can't test on windows as I don't use it):
The first time that aircraft type is encountered in a filename, it will create the new directory.
Example program TryAircraftPaths.py shows how to use the AircraftPaths class
As written, homepath will be in source code directory as 'All_My_Aircraft', this can be changed as required.
So in your program:
add two lines:
and instantiate class:
then:
will return save path
AircraftPaths.py:
will work with all file suffixes as written, searches filename stem for aircraft type.
Automatically creates directory if it doesn't already exist.
Won't harm existing directories.
change homepath to your base directory, for your code should, this should be (I can't test on windows as I don't use it):
homepath = Path("C:/Users/Edward")to add new aircraft (and auto create directories) add entry to 'self.aircraft_type' dictionary.
The first time that aircraft type is encountered in a filename, it will create the new directory.
Example program TryAircraftPaths.py shows how to use the AircraftPaths class
As written, homepath will be in source code directory as 'All_My_Aircraft', this can be changed as required.
So in your program:
add two lines:
import AircraftPaths
and instantiate class:
ap = AircraftPaths.AircraftPaths()
then:
ap.get_aircraft_path(zipname)
will return save path
AircraftPaths.py:
from pathlib import Path import os class AircraftPaths: def __init__(self): # Assures starting directory is source os.chdir(os.path.abspath(os.path.dirname(__file__))) # Set this equal to you're base path (top level where homepath = Path('.') self.basepath = homepath / 'All_My_Aircraft' self.basepath.mkdir(exist_ok=True) self.aircraft_type = { '737-400': { 'subpath': 'Boeing', 'aircraft': 'B737-400' }, '737-800': { 'subpath': 'Boeing', 'aircraft': 'B737-800' }, '320-200': { 'subpath': 'Airbus', 'aircraft': 'A320-200' }, '195-200': { 'subpath': 'Embraer', 'aircraft': 'E195-200' }, 'general': { 'subpath': 'PAI Aircraft', 'aircraft': 'Misc' } } def get_aircraft_path(self, zipfilename): for code, details in self.aircraft_type.items(): if code in zipfilename: ap = self.basepath / f"{details['subpath']}" ap.mkdir(exist_ok = True) airpath = ap / f"{details['aircraft']}" airpath.mkdir(exist_ok = True) return airpath ap = self.basepath / f"{self.aircraft_type['general']['subpath']}" ap.mkdir(exist_ok = True) airpath = ap / f"{self.aircraft_type['general']['aircraft']}" airpath.mkdir(exist_ok = True) return airpath if __name__ == '__main__': ap = AircraftPaths() print(ap.aircraft_type)TryAircraftPaths.py
import AircraftPaths def main(): ap = AircraftPaths.AircraftPaths() zipfilelist = [ "ziggy195-200theMonster.zip", "Caltech320-200hacked.zip", "UnknownAircraft.zip", ] for zipname in zipfilelist: path = ap.get_aircraft_path(zipname) print(f"Path to save file in is: {path.resolve()}") if __name__ == '__main__': main()running test program TryAircraftPaths will display (set your homepath in AircraftPaths.py before running):
Output:Path to save file in is: .../projects/T-Z/T/TryStuff/src/Paths/All_My_Aircraft/Embraer/E195-200
Path to save file in is: .../projects/T-Z/T/TryStuff/src/Paths/All_My_Aircraft/Airbus/A320-200
Path to save file in is: .../projects/T-Z/T/TryStuff/src/Paths/All_My_Aircraft/PAI Aircraft/Misc