Reputation: 99
I am writing a Pokemon game and I want to make folders to save different types of Pokemon, as well as other sorts of information, in. I want to use folders because it would be really messy if I were to save all my data into a single file.
Is it possible to create folders with a Python program? This would make it easier and cleaner for me when I try to import the Pokemon data from external websites.
Upvotes: 0
Views: 127
Reputation: 26
You can run any command that you want with python by doing:
import os
os.popen("mkdir random_name") # This creates a directory called "random_name"
os.popen("touch rando_name.txt") # This creates a file called "random_name"
You can run any command that you would usually run in terminal inside the popen.
You can use the popen() command in both UNIX(Linux, macOS) as well as Windows OS. You can learn more regarding this by taking a look in the python documentation. https://docs.python.org/2/library/subprocess.html
Upvotes: 1
Reputation: 44886
If you'd like to create folders (or directories), you need os.mkdir
:
import os
os.mkdir("folder_name")
To create some deep folders at once, use os.makedirs
:
os.makedirs("path/to/something")
Then all that structure of three folders will be created.
Tutorialspoint has a short tutorial about os.mkdir
.
Upvotes: 1
Reputation: 19816
You can use with statement
like this:
with open('some_file_100.txt', 'a') as f:
pass
The above will just create an empty file, if you want to write something to the created file, you can try:
with open('some_file_100.txt', 'a') as f:
f.write('some text')
When you use with statement
you do not need to close your files explicitly because they are closed autmatically at the end of the block.
Upvotes: 0
Reputation: 9763
You can use open
with the a
mode, which opens a file in append mode, and creates it if it does not exist:
my_file = open('file.txt', 'a')
# Optionally: write stuff to my_file, using my_file.write('stuff')
my_file.close()
Upvotes: 2