dealing with le systems
play

Dealing with le systems COMMAN D LIN E AUTOMATION IN P YTH ON - PowerPoint PPT Presentation

Dealing with le systems COMMAN D LIN E AUTOMATION IN P YTH ON Noah Gift Lecturer, Northwestern & UC Davis & UC Berkeley | Founder, Pragmatic AI Labs Computer User log les build artifacts directory trees structured data


  1. Dealing with �le systems COMMAN D LIN E AUTOMATION IN P YTH ON Noah Gift Lecturer, Northwestern & UC Davis & UC Berkeley | Founder, Pragmatic AI Labs

  2. Computer User log �les build artifacts directory trees structured data unstructured data ML models COMMAND LINE AUTOMATION IN PYTHON

  3. Filesystem File system is a hierarchy The Unix tree command ??? Makefile ??? README.md ??? demos ? ??? flask-sklearn ? ? ??? Dockerfile ? ? ??? Makefile ? ? ??? README.md ? ? ??? app.py ? ? ??? ml_prediction.joblib COMMAND LINE AUTOMATION IN PYTHON

  4. Human User con�g �les user pro�le data business documents code data science projects ML models COMMAND LINE AUTOMATION IN PYTHON

  5. Leaning into os.walk os.walk returns: root dirs files Returns a generator # generator only returns a result at a time foo = os.walk("/tmp") type(foo) generator COMMAND LINE AUTOMATION IN PYTHON

  6. Finding �le extensions splitting off a �le extension fullpath = "/tmp/somestuff/data.csv" _, ext = os.path.splitext(fullpath) '.csv' COMMAND LINE AUTOMATION IN PYTHON

  7. Let's practice. COMMAN D LIN E AUTOMATION IN P YTH ON

  8. Find �les matching a pattern COMMAN D LIN E AUTOMATION IN P YTH ON Noah Gift Lecturer, Northwestern & UC Davis & UC Berkeley | Founder, Pragmatic AI Labs

  9. Using Path.glob() Path.glob() �nds patterns in directories yields matches can recursively search COMMAND LINE AUTOMATION IN PYTHON

  10. Simple glob patterns from pathlib import Path path = Path("data") list(path.glob("*.csv")) [PosixPath('mydata.csv'), PosixPath('yourdata.csv')] COMMAND LINE AUTOMATION IN PYTHON

  11. Recursive glob patterns from pathlib import Path path = Path("data") list(path.glob("**/*.csv")) [PosixPath('data/one.csv'), PosixPath('data/moredata/two.csv')] COMMAND LINE AUTOMATION IN PYTHON

  12. Using os.walk to �nd patterns os.walk pattern matching more explicit can explicitly look at directories or �les doesn't return Path object import os result = os.walk("/tmp") # consume the generator next(result) # Find your pattern here.... COMMAND LINE AUTOMATION IN PYTHON

  13. Using fnmatch Supports Unix shell wildcard matches Can be converted to regular expression if fnmatch.fnmatch(file, "*.csv"): log.info(f"Found match {file}") COMMAND LINE AUTOMATION IN PYTHON

  14. Converting fnmatch to regular expression fnmatch.translate converts pattern to regex import fnmatch, re regex = fnmatch.translate('*.csv') pattern = re.compile(regex) print(pattern) re.compile(r'(?s:.*\.csv)\Z', re.UNICODE) pattern.match("titanic.csv") <re.Match object; span=(0, 11), match='titanic.csv'> COMMAND LINE AUTOMATION IN PYTHON

  15. Let's practice! COMMAN D LIN E AUTOMATION IN P YTH ON

  16. High-level �le and directory operations COMMAN D LIN E AUTOMATION IN P YTH ON Noah Gift Lecturer, Northwestern & UC Davis & UC Berkeley | Founder, Pragmatic AI Labs

  17. Two powerful modules shutil : high-level �le operations copy tree delete tree archive tree tempfile : generates temporary �les and directories COMMAND LINE AUTOMATION IN PYTHON

  18. Using shutil.copytree Can recursively copy a tree of �les and folders from shutil import copytree, ignore_patterns Can ignore patterns copytree(source, destination, ignore=ignore_patterns('*.txt', '*.excel')) COMMAND LINE AUTOMATION IN PYTHON

  19. copytree in action In [1]: pwd Out[1]: '/private/tmp' In [2]: !mkdir sometree && touch sometree/somefile.txt In [3]: from shutil import copytree In [5]: copytree("sometree", "newtree") Out[5]: 'newtree' In [6]: !ls -l newtree/ total 0 -rw-r--r-- 1 noahgift wheel 0 May 19 20:08 somefile.txt COMMAND LINE AUTOMATION IN PYTHON

  20. Using shutil.rmtree Can recursively delete tree of �les and folders from shutil import rmtree rmtree(source, destination) COMMAND LINE AUTOMATION IN PYTHON

  21. Using shutil.make_archive Archiving a tree with make_archive from shutil import make_archive make_archive("somearchive", "gztar", "inside_tmp_dir") '/tmp/somearchive.tar.gz' COMMAND LINE AUTOMATION IN PYTHON

  22. Automation Takeaways Use the Python standard library If an automation tasks requires a lot of code The approach may be incorrect Consult the Python standard library Look at 3rd party Python libraries The less code you write, the less bugs you have COMMAND LINE AUTOMATION IN PYTHON

  23. Practicing high-level automation COMMAN D LIN E AUTOMATION IN P YTH ON

  24. Using pathlib COMMAN D LIN E AUTOMATION IN P YTH ON Noah Gift Lecturer, Northwestern & UC Davis & UC Berkeley | Founder, Pragmatic AI Labs

  25. Using pathlib.Path from pathlib import Path Make a path object path = Path("/usr/bin") List items in directory as object list(path.glob("*"))[0:4] [PosixPath('/usr/bin/link'), PosixPath('/usr/bin/tput'), COMMAND LINE AUTOMATION IN PYTHON

  26. Working with PosixPath objects mypath.cwd() PosixPath('/app') mypath.exists() True COMMAND LINE AUTOMATION IN PYTHON

  27. More PosixPath mypath.as_posix() '/usr/bin/link' COMMAND LINE AUTOMATION IN PYTHON

  28. Open a �le with pathlib Open a Makefile from a path object from pathlib import Path some_file = Path("Makefile") Print the last line of the Makefile with some_file.open() as file_to_read: print(file_to_read.readlines()[-1:]) ['all: install lint test\n'] COMMAND LINE AUTOMATION IN PYTHON

  29. Create a directory with pathlib Path objects can create directories from pathlib import Path tmp = Path("/tmp/inside_tmp_dir") tmp.mkdir() Contents of the directory ls -l /tmp/ inside_tmp_dir/ COMMAND LINE AUTOMATION IN PYTHON

  30. Write text with pathlib write_text() is a serious shortcut write_path = Path("/tmp/some_random_file.txt") write_path.write_text("Wow") 3 print(write_path.read_text()) 'Wow' COMMAND LINE AUTOMATION IN PYTHON

  31. Rename a �le with pathlib renaming a �le with pathlib from pathlib import Path # Create a Path object modify_file = Path("/tmp/some_random_file.txt") #rename file modify_file.rename("/tmp/some_random_file_renamed.txt") ls /tmp some_random_file_renamed.txt COMMAND LINE AUTOMATION IN PYTHON

  32. Practicing with pathlib COMMAN D LIN E AUTOMATION IN P YTH ON

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend