Skip to content

Instantly share code, notes, and snippets.

@poacosta
Last active February 7, 2023 11:54
Show Gist options
  • Save poacosta/1f4b0185135b6038e3129a69e4981749 to your computer and use it in GitHub Desktop.
Save poacosta/1f4b0185135b6038e3129a69e4981749 to your computer and use it in GitHub Desktop.
Create files by extensions given a path using Python
import os
EXTENSIONS = ['py', 'rb', 'cs', 'ex', 'js', 'go', 'rs', 'dart', 'php', 'sql']
def create_files_by_extension_given_a_filename(name, directory):
for extension in EXTENSIONS:
file = f"{name}.{extension}"
if os.path.exists(os.path.join(directory, file)):
print('{0}: already exists!'.format(file))
else:
with open(os.path.join(directory, file), 'w'):
pass
if __name__ == '__main__':
path = r"/<desired-path>/"
filename = "name_of_file"
create_files_by_extension_given_a_filename(filename, path)
print("Done!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment