1 / 7

Files and Directory Manipulation

Files and Directory Manipulation. CSIS 1595: Fundamentals of Programming and P roblem Solving 1. OS Module. Commands for manipulating files and directories Similar to Unix syntax import os required Example: Directory commands os.getcwd () Returns string with current path

tanith
Télécharger la présentation

Files and Directory Manipulation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Files and Directory Manipulation CSIS 1595: Fundamentals of Programming and Problem Solving 1

  2. OS Module • Commands for manipulating files and directories • Similar to Unix syntax • import osrequired • Example: Directory commands • os.getcwd()Returns string with current path • os.chdir(path) Move to directory path • Absolute path or relative path (or .. for parent directory)

  3. File Manipulation • os.listdir()Returns list of strings with names of all files/subdirectories in current directory • os.path.isfile(“filename”)Returns true if filename is a file in the current directory • os.path.isdir(“dirname”)Returns true if dirnameis a file in the current directory

  4. Listing all Files/Directories

  5. Uses for Validation • Can check whether file exists before openingif os.path.isfile(filename):… • Not a substitute for exception handling(file may exist but still not be readable) • Can use to check whether file already exists before overwriting it • Verify with user

  6. Validating File Write

  7. Navigation Example • Can create file navigation tool • List all files/directories and prompt user for selection • If file open it, or if directory navigate to it

More Related