473,394 Members | 1,709 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

adding current date to a file name in a python script

Hello,

I am trying to add the current date to a file name in python script
like thus:

import os
import sys
import rpm
import time
import datetime

today = datetime.date.today()
print "The date is", today
myFile = '/work/output1'
myFile = myFile.join(today)
myFile = myFile.join(".txt")

print "myFile is",myFile
running the scripts indeed prints the correct date ,
but afterwards there is the following error:
The date is 2006-01-29
Traceback (most recent call last):
File "addDate.py", line 13, in ?
myFile = myFile.join(today)
TypeError: sequence expected, datetime.date found

How should I do this ?

Regards,
MR

Jan 29 '06 #1
2 21048
ma******@gmail.com enlightened us with:
myFile = '/work/output1'
myFile = myFile.join(today)
myFile = myFile.join(".txt")


join does something completely different. You want:

myFile = '/work/output1/%s.txt' % today

Sybren
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don't we just take the
safety labels off of everything and let the problem solve itself?
Frank Zappa
Jan 29 '06 #2
ma******@gmail.com wrote:
I am trying to add the current date to a file name in python script
like thus:

import os
import sys
import rpm
import time
import datetime

today = datetime.date.today()
print "The date is", today
myFile = '/work/output1'
myFile = myFile.join(today)
myFile = myFile.join(".txt")

print "myFile is",myFile
running the scripts indeed prints the correct date ,
but afterwards there is the following error:

The date is 2006-01-29
Traceback (most recent call last):
File "addDate.py", line 13, in ?
myFile = myFile.join(today)
TypeError: sequence expected, datetime.date found

How should I do this ?


datetime.date.today() returns a date object, not a string.

if the default string conversion is what you want, use str(today) to
convert to a string.

also note that "join" doesn't do what you think it does; "x.join(y)"
joins all members of the sequence y (which must all be strings) using
x as the separator. this gives you a filename like

'.2/work/output10/work/output10/work/output16/work/output1-/work/output10/work/o
utput11/work/output1-/work/output12/work/output19t2/work/output10/work/output10/
work/output16/work/output1-/work/output10/work/output11/work/output1-/work/outpu
t12/work/output19x2/work/output10/work/output10/work/output16/work/output1-/work
/output10/work/output11/work/output1-/work/output12/work/output19t'

which is probably not what you want.

to fix this, you can use +=

myFile = '/work/output1'
myFile += str(today)
myFile += ".txt"

or % formatting:

myFile = '/work/output1%s.txt' % today

(here, "%s" does the str() call for you).

</F>

Jan 29 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: jim.kelly | last post by:
Hi all, I'm having trouble with the following line of code in my php script: $result = mysql_query('LOAD DATA LOCAL INFILE "NYSE_".$today.".txt" INTO TABLE main FIELDS TERMINATED BY "," LINES...
3
by: SeeBelow | last post by:
I have not been able to find a simple way to have a python script execute an MS-DOS program repeatedly. execv(), for example, won't run it more than once. I have been using many lines of a .bat...
13
by: Luigi | last post by:
Imagine I have a small, static, personal site (I do, actually: http://kirpi.it/). And imagine that, just below the title of the page, I would like to have a comment, or a news line, or a birthday...
3
by: Reginald Blue | last post by:
stupid question: RemotingConfiguration.Configure("MyProject.exe.config"); (where the actual EXE that's built is named "MyProject.exe", of course). is there any way to do that without hard...
6
by: tatamata | last post by:
Hello. How can I run some Python script within C# program? Thanks, Zlatko
2
by: Viewer T. | last post by:
I am trying to write a script that deletes certain files based on certain criteria. What I am trying to do is to automate the process of deleting certain malware files that disguise themselves...
3
by: n o s p a m p l e a s e | last post by:
Suppose I have a batch file called mybatch.bat and I want to run it from a python script. How can I call this batch file in python script? Thanx/NSP
8
by: sandeep | last post by:
hi all i am very much a newbie to python but has some experience of development.i am trying to write a script which will loop through the outlook 2003 inbox and save the email data in an simple...
1
by: kayox007 | last post by:
hello everyone, please i need help, i intend uploading 5 pictures all at once with each file fields named pic 1 to pic 5, the first field is compulsory , but am finding it hard getting their file...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.