python - Creating and naming files with a while function -


I am trying to create a file, for each day of a year, and I was thinking that I For this or for but since I am mixing numbers and letters, this does not work since then.

  def Create File (): date = 101 # This is considered 0101 (first of Januar, but since I can not start with 0, this should be the other option) Date and date = 131: name = (date) + '. Txt 'date + = 1 CreateFile ()  

You can not add strings and integers :

  name = date + '.txt' # typeError  

but you can use to create a filename:

 name = "{0} .txt" .format (date)  

str.format gives you four digits Permission to apply:

  gt; & Gt; "{0: 04d} .txt". Format (101) '0101.txt'  

(See more on formatting options).

In the end, seeing how often you will loop, I will recommend the loop loop to start, start and To avoid having a date manually:

category (101, 132): name = "{0: 04d} .txt". Format (date) ...

Comments

Popular posts from this blog

import - Python ImportError: No module named wmi -

Editing Python Class in Shell and SQLAlchemy -

c# - MySQL Parameterized Select Query joining tables issue -