Created
October 26, 2020 11:58
-
-
Save pandorica-opens/321c811c71e3ade2c2415eb48c2e935c to your computer and use it in GitHub Desktop.
The new_directory function creates a new directory inside the current working directory, then creates a new empty file inside the new directory, and returns the list of files in that directory.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import os | |
| def new_directory(directory, filename): | |
| # Before creating a new directory, check to see if it already exists | |
| if os.path.isdir(directory) == False: | |
| os.mkdir(directory) | |
| # Create the new file inside of the new directory | |
| os.chdir(directory) | |
| if os.path.isfile(filename) == False: | |
| os.mknod(filename) | |
| os.chdir('..') | |
| # Return the list of files in the new directory | |
| return os.listdir(directory) | |
| print(new_directory("PythonPrograms", "script.py")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment