Reputation: 21
i want to create a new .txt file every time i run the python script. Any idea how to do that . My current code :
import datetime
def write_up_commands(up_int):
fp = open(datetime.datetime.now().strftime("20%y-%m-%d %H:%M:%S") + '_abc.txt', 'w+')
for i in up_int:
fp.write('int ' + i + '\n mode \n')
fp.close()
something like
1_abc
2_abc
3_abc
can i add a date/timestamp ?
Error :
IOError: [Errno 22] invalid mode ('w+') or filename: '2018-09-10 18:02:40_abc.txt'
For user input :
def write_up_commands(up_int):
user_text = input("write the name of the file: ")
fp = open(datetime.datetime.now().strftime("%Y-%m-%d %H.%M.%S") + user_text + '.txt', 'w+')
#with open(datetime.datetime.now().strftime("%Y-%m-%d %H.%M.%S") + user_text + '.txt', 'w+') as fp:
for i in up_int:
fp.write('int ' + i + '\n mode \n')
NameError: name 'jinni' is not defined
Upvotes: 0
Views: 637
Reputation: 2955
The error that you're reporting is caused by the :
in the folder's name. Replace that with a valid character:
import datetime
def write_up_commands(up_int):
fp = open(datetime.datetime.now().strftime("%Y-%m-%d %H.%M.%S") + '_abc.txt', 'w+')
for i in up_int:
fp.write('int ' + i + '\n mode \n')
fp.close()
Let's check what is going on:
with datetime.datetime.now()
we ask the time of now
, so the current time
with .strftime("%Y-%m-%d %H.%M.%S")
we format the time from the datetime
module as we prefea: %Y
means 4 digits year, then we want a -
, then a two digits month %m
, then -
, two digits day %d
, space , 24h format hours
%H
, dot .
, 2 digits minutes %M
, dot .
, two digit seconds %s
.
You can print the current datetime with:
print(datetime.datetime.now().strftime("%Y-%m-%d %H.%M.%S"))
with + '_abc.txt'
we add at the datetime the '_abc.txt'
text.
You can print the full text with:
print(datetime.datetime.now().strftime("%Y-%m-%d %H.%M.%S") + '_abc.txt')
with open('my_file.txt', 'w+')
we ask our operating system to create a file with the name my_file.txt
. w+
Opens a file for both writing and reading. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.
with
for i in up_int:
fp.write('int ' + i + '\n mode \n')
fp.close()
you write in your file 'int ' + i + '\n mode \n'
for each i in up_int.
If we want to create a file with a different name, we can modify the (datetime.datetime.now().strftime("%Y-%m-%d %H.%M.%S") + '_abc.txt'
line. If for example we want a file with: current date, underscore, user input, .txt we can write:
user_text = raw_input("write the name of the file: ")
fp = open(datetime.datetime.now().strftime("%Y-%m-%d %H.%M.%S") + user_text + '.txt', 'w+')
...
Even if not asked, I'd like to point out that fp.close()
is the line which closes the file. It is dangerous to handle this method like that, because if you forget to close the file or something happens before the file closes, you will leave open files all around. I suggest you to change all your code with:
with open(datetime.datetime.now().strftime("%Y-%m-%d %H.%M.%S") + user_text + '.txt', 'w+') as fp:
for i in up_int:
fp.write('int ' + i + '\n mode \n')
which automatically handles the closing procedure in any situation.
For your second question, you actually are creating a new file each time. Keep in mind that, since the file's name changes with the time, if you create more than 1 file each second, each file will overwrite the previous one.
As a side note, notice that I've changed 20%y
with %Y
, which is the correct way to display the 4-digit year
Upvotes: 1