Search code examples
python-3.xpathlib

How do I use pathlib to create files named for range of dates?


I can print a range of filenames with the below code but I need to write empty files instead of printing. I know that pathlib.Path.touch and accomplish this but . . . do I need to define a method for the touch?

import datetime
import pathlib

for i in range(0, 180):
    print((datetime.date.today() + datetime.timedelta(i)).strftime("%Y%m%d" + ".exml"))

Solution

  • I don't think you need to define a method. Try this:

    import datetime
    import pathlib
    
    for i in range(0, 180):
        fname = (datetime.date.today() + datetime.timedelta(i)).strftime("%Y%m%d" + ".exml")
        print((fname))
        pathlib.Path(fname).touch()