Create file in sub directory Python?

Create file in sub directory Python?

Store the created directory in a variable. os.mkdir throws if a directory exists by that name.
Use os.path.join to join path components together (it knows about whether to use / or ).

import os.path

subdirectory = datetime + -dst
try:
    os.mkdir(subdirectory)
except Exception:
    pass

for ip in open(list.txt):
    with open(os.path.join(subdirectory, ip.strip() + .txt), a) as ip_file:
        ...

first convert the datetime to something the folder name can use
something like this could work
mydate_str = datetime.datetime.now().strftime(%m-%d-%Y)

then create the folder as required
– check out
Creating files and directories via Python
Johnf

Create file in sub directory Python?

Leave a Reply

Your email address will not be published. Required fields are marked *