Skip to content

Instantly share code, notes, and snippets.

@pandorica-opens
Created October 26, 2020 11:58
Show Gist options
  • Select an option

  • Save pandorica-opens/321c811c71e3ade2c2415eb48c2e935c to your computer and use it in GitHub Desktop.

Select an option

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.
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