Presentation is loading. Please wait.

Presentation is loading. Please wait.

Python: File Directories What is a directory? A hierarchical file system that contains folders and files. Directory (root folder) Sub-directory (folder.

Similar presentations


Presentation on theme: "Python: File Directories What is a directory? A hierarchical file system that contains folders and files. Directory (root folder) Sub-directory (folder."— Presentation transcript:

1 Python: File Directories What is a directory? A hierarchical file system that contains folders and files. Directory (root folder) Sub-directory (folder inside of another folder) File (contains information and data)

2 Python: File Directories What is a path? The location of a file or folder. Each level is separated by a backslash (\) such as this example: \Directory\Sub-directory\File.ext

3 Python: File Directories Portable Python automatically looks in the current working directory to import files. What if we want to import a file or image that is stored in a different directory?

4 Python: File Directories Let's look at some functions in the “os” module. os.getcwd() This function returns the path of the current working directory.

5 Python: File Directories Start a new module called “file directory” and try running the following code to find the path of your current working directory. import os cwd_path=os.getcwd() print("My current working directory is",cwd_path)

6 Python: File Directories os.listdir(path) This function returns a list containing the names of the folders and files inside the directory path. Use cwd_path to find and print the names of the folders and files in your current working directory.

7 Python: File Directories Save a copy of the butterfly gif file on your desktop and rename as “bfly_desk”. In your module, add code below and run. from sgfx import * w=Window() w.gif(250,250,'bfly_desk.gif') w.run() Did the butterfly appear? Why or why not?

8 Python: File Directories In your module, set a variable string to the gif directory path. Inside a string, a backslash (\) is used to start an "escape sequence" which has special meaning to Python. Here are some variations of the same path: new_path=r”C:\Desktop\New_Directory” new_path=”C:\\Desktop\\New_Directory“\\Desktop\\New_Directory new_path=”C:/Desktop/New_Directory”

9 Python: File Directories Now add the following function os.chdir() to change your current working directory. import os new_path=”C:/Desktop/New_Directory”C:/Desktop/New_Directory os.chdir(new_path) Run again. Did the butterfly open this time? Check and print the current working directory again.

10 Python: File Directories You can create new directory inside the current working directory using os.mkdir(“”) Or remove a directory using os.rmdir(“”)

11 Python: File Directories Start a new module named “writing”. Set the current working directory to the desktop. Create a directory called “text_files” and print os.listdir() to verify that it was added.

12 Python: File Directories The function os.path.join() adds a sub- directory to the end of a path. Example: os.path.join(cwd_path,”text_files”) Use this function and change the current working directory to “text_files”.

13 Python: File Directories Now create a text file named “odd” in the “text_files” directory using the open function. text_doc=open() Remember to close the file! Print this function at the end to confirm file is closed. text_doc.closed Returns True when file is closed Returns False when file is still open

14 Python: File Directories text_doc=open(“odd.txt”,”?”) Permission modes: “w” write-only mode “r” read-only mode “a” append mode Modify your function to use write-only mode.

15 Python: File Directories Ready to write! Use a loop and the write function below to write the odd numbers between 0 and 10 each on a new line inside “odd”. Convert numbers to a string character for text file. text_doc.write()

16 Python: File Directories Add another open function using append mode and add a text string at the end “The odds are good.” Use read mode to print the results to the Interpreter. Open the text file to compare.

17 Python: File Directories Rename file function os.rename(“current_name”,”new_name”) Use to rename “odd” to “even” and replace the content with the even numbers between 0 and 10.

18 Python: File Directories Remove file function os.remove(“name”) Use to delete “text_files” when you are finished.


Download ppt "Python: File Directories What is a directory? A hierarchical file system that contains folders and files. Directory (root folder) Sub-directory (folder."

Similar presentations


Ads by Google