adding current date to a file name in a python script

  • Permalink
  • submit to reddit
  • Email
  • Follow


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

0
Reply markryde (46) 1/29/2006 9:06:35 AM

See related articles to this posting


markryde@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
0
Reply sybrenUSE8145 (706) 1/29/2006 9:11:54 AM

markryde@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>



0
Reply fredrik2101 (5275) 1/29/2006 9:27:16 AM
comp.lang.python 73394 articles. 14 followers. Post

2 Replies
230 Views

Similar Articles

[PageSpeed] 42


  • Permalink
  • submit to reddit
  • Email
  • Follow


Reply:

Similar Artilces:

Adding date to a file name
Hello everyone, Can anyone tell me if it's possible for me to add the current date to a file name being created in an ODS statement? I'd like to setup my program so that when anyone else runs it, the date that they run it is automatically attached to the RTS file name. For instance, today's file would be called "InvName060904". I tried using a let statement, and was able to create the proper value as follows: %let date = compress(put(today(),mmddyy8.),'/'); However, it's not working when I substitute this in the RTF file name, as follows: ODS RTF FILE ...

Log file name with current date
Hi All, =A0I want to add current date value in the log file name. The reason is.. i= am running a SAS program on daily basis. my SAS log overrites every day.. = for Audit purpose i want to keep all the log file with date on that log fil= e name. Thank you=A0 in advance. Thanks 'n' Regards=20 alex .S.=0A=0A=0A Now, send attachments up to 25MB with Yahoo! India M= ail. Learn how. http://in.overview.mail.yahoo.com/photos Hi Alex, Try this.. PROC PRINTTO LOG="D:\Production_jobs\logs\Prog&sysdate..TXT"; RUN; Thanks, shiva ...

naming file using `date` in script
Hi, I'm trying to do a small script which will create a tarball with the date in it. today="`date`" tar -zcf tarball_$today.tar.gz <listoffiles> ... but of course, it doesn't work. I get 'can't stat' etc. I did this years ago, and IIRC, it involved 'cut', but I'm damned if I can remember how.. Anyone help? Thanks. in comp.unix.shell i read: >I'm trying to do a small script which will create a tarball with the date >in it. > >today="`date`" > >tar -zcf tarball_$today.tar.gz <listoffiles> there i...

Get Current Script File Name
How do you get the name of the currently execting script? By this I mean the name of the include file that is being executed: File: include.php <?php //Call some function to display include.php or whatever my filename is ?> File: holder.php <?php inlcude( "include.php" ); ?> I tried everything in the $_SERVER variable but to no avail - Googling it doesn't help as it all points to either the include method or the $_SERVER variable. These all just give me 'holder.php' or similar. I want this as I want to create a LOGGER class similar to the one in Java to ...

Re: Adding date to a file name
I think you need to add two periods after &date, as the macro processor will eat the first one, as it is written now you will end up with: ODS RTF FILE = "InvName060904RTF"; <from ODS RTF FILE = "InvName&date.RTF";> And I think you want: ODS RTF FILE = "InvName060904.RTF"; <from ODS RTF FILE="InvName&date..RTF";> HTH Toby Dunn -----Original Message----- From: SAS(r) Discussion [mailto:SAS-L@LISTSERV.UGA.EDU] On Behalf Of Melanie J. Sent: Wednesday, June 09, 2004 3:05 PM To: SAS-L@LISTSERV.UGA.EDU Subject: Adding date to a file n...

Adding a date and timestamp to file name
Morning everybody I need to make a file with a date and time stamp so that the file name should look like this: XXXXXXX_YYYYMMDD_HHMMSS.txt I am able to make a macro for the date and the hour and the minutes but I have problems with the seconds. Anyone has any ideas? Best regards, Styrmir Gunnarsson Risk and Collection Analyst Ekspres Bank Denmark On Jun 18, 11:29=A0am, Styrmir <styrm...@gmail.com> wrote: > Morning everybody > > I need to make a file with a date and time stamp so that the file name > should look like this: > XXXXXXX_YYYYMMDD_HHMMSS.txt > > I am a...

Adding Dates to Multiple File Names
I'd like to rename a group of files with the date in the file name. I'm not quite sure how to do that. I have figured out how to set a date to a variable, but the renaming bit is eluding me. FOR /F "TOKENS=1,2*" %%A IN ('DATE/T') DO SET TODAY=%%B SET TODAY=%TODAY:/=_% copy C:\*.iut C:\Test ren C:\Test*.iut %TODAY%_*.IUT The files in question have names like this: 002.IUT I'd like them to be renamed like this: 02_01_2005_002.IUT The result from my dos command is 02_01_2005.IUT - leaving out the original part of the file name, and then resu...

Re: Adding date to a file name #7
Hi Melanie, you don't need the left() nor the compress() Just use format mmddyyN6 data _null_; call symput("date", put("&sysdate"d,mmddyyN6.) ); run; without a datastep, one alternative is %let date= %sysfunc( date(), mmddyyN6 ) ; Peter Crawford On Thu, 10 Jun 2004 05:54:19 -0700, Melanie J. <mbarish@PROGENICS.COM> wrote: >Thanks to everyone who replied - I needed to use a combination of the >&sysdate and the double dots. This is the final code that I used: > >data _null_; > call symput("date",left(compress(put("&...

Re: Adding date to a file name #4
you need snakeeyes: double dots: replace this > ODS RTF FILE = "InvName&date.RTF"; with > ODS RTF FILE = "InvName&date..RTF"; ................................^ the left eye is the delimiter for the macro variable Date the right eye is the delimiter between filename and extension Ron Fehd the macro maven CDC Atlanta GA USA RJF2@cdc.gov remember perspective: the error is not always where it seems to occur! -- RJF2 > -----Original Message----- > From: Melanie J. [mailto:mbarish@PROGENICS.COM] > Sent: Wednesday, June 09, 2004 4:05 PM > To: SAS-...

Re: Log file name with current date
You can use &sysdate or &sysdate9 macro variables added to the log file name. Sent on the Now Network� from my Sprint® BlackBerry -----Original Message----- From: Alex S <alex4sas@YAHOO.CO.IN> Date: Thu, 29 Oct 2009 16:30:36 To: <SAS-L@LISTSERV.UGA.EDU> Subject: Log file name with current date Hi All, � I want to add current date value in the log file name. The reason is.. i am running a SAS program on daily basis. my SAS log overrites every day.. for Audit purpose i want to keep all the log file with date on that log file name. Thank you� in advance. ...

Add current date to end of file name
Hi, Does anyone know if it's possible/how to add the current date at the end of a file name, I have syntax that I run repeatedly that is saving files as a specific name. Each time I run it I'd like to give it a different name without having to manually change it each time. For ex, syntax to look like: SAVE OUTFILE='B/Folder1/File_092806 Any help or other ideas would be greatly appreciated. Mimi Hi Mimi, Thanks to Raynald Levesque for providing the basic for this script. The following script will add current date to the end of the file for any file and wi...

Re: Log file name with current date #6
So once you use this command (in PC SAS), how do you get it to stop? Short of closing out PC SAS? ...

Re: Log file name with current date #3
> From: Alex S > Subject: Log file name with current date > > Hi All, > I want to add current date value in the log file name. The > reason is.. i am running a SAS program on daily basis. my SAS > log overrites every day.. for Audit purpose i want to keep > all the log file with date on that log file name. > > Thank you in advance. > > Thanks 'n' Regards > alex .S. Here are your choices: * log * altlog * printto log= I recommend altlog; then you have the current log and your date-stamped logs. log and altlog are command-line options Here is ...

Need Help
Version FM12 Still having issues creating a script that puts the current date in a file name. The current script I have saves the export to a PDF. When you specify the output file name in Specify Output File I don't see any other options? Any help would be greatly appreciated! Tom E <tevanoff65@gmail.com> wrote: > Version FM12 > > Still having issues creating a script that puts the current date in a > file name. The current script I have saves the export to a PDF. When you > specify the output file name in Specify Output File I don't see any &g...

Re: Log file name with current date #4
Hi Alex, In the previous post below it shows you how you can get time chronology in directory sortable order by having yyyymmddhhmmss as decending units of measure for the timestamp in your filename. see: http://listserv.uga.edu/cgi-bin/wa?A2=ind0903C&L=sas-l&P=R32899 Hope this is helpful. Mark Terjeson Investment Business Intelligence Investment Management & Research Russell Investments 253-439-2367 Russell Global Leaders in Multi-Manager Investing > From: Alex S > Subject: Log file name with current date > > Hi All, > I want to add current date val...

Re: Log file name with current date #2
This is controlled with the LOGPARM option. You will need to consult the documentation to figure out the details for your specific application. Espically the section "Directives for Controlling the Name of SAS Log Files". LOGPARM= "<OPEN= APPEND | REPLACE | REPLACEOLD> <ROLLOVER= AUTO | NONE | SESSION> <WRITE= BUFFERED | IMMEDIATE>" On 10/29/09, Alex S <alex4sas@yahoo.co.in> wrote: > Hi All, > I want to add current date value in the log file name. The reason is.. i am running a SAS program on daily basis. my SAS log overrites every day.....

Re: Log file name with current date #7
Good evening=2C =20 Here is another solution utilizing SAS DM commands. Code could be inserted = at the end of the program or at any point where you want your log sent to e= xternal file: =20 filename logfile "C:\workfolder\log_&sysdate9..log"=3B =20 dm log 'file logfile replace' output =3B=20 =20 It saves the log at the external location while still keeping it in the SAS= log window.=20 Hope it helps =20 Thanks=20 =20 Ruslan =20 > Date: Thu=2C 5 Nov 2009 18:22:15 -0500 > From: art297@NETSCAPE.NET > Subject: Re: Log file name with current date &g...

Re: Log file name with current date #5
Norm, I think you can do what you want simply by running: proc printto; run; HTH, Art ------- On Thu, 5 Nov 2009 18:12:29 -0500, Norman Weston <nweston@AMGEN.COM> wrote: >So once you use this command (in PC SAS), how do you get it to stop? >Short of closing out PC SAS? ...

insert python script in current script
I was wondering is there any way to do this: I have written a class in python and __init__ goes like this: def __init__(self): self.name =3D 'jack' self.age =3D 50 import data now here there is data.py in the same directory and contents are like: self.address =3D 'your address' self.status =3D 'single' The problem is 'self' is giving some error here. I need to know if somehow I can do this. It's like inserting the script as it's part of the file itself. Cheers On 16 avr, 09:42, "Prashant" <shikha_saxena2...@yahoo.com> wrote: ...

Help Needed with Export PDF With Current Date in the File Name
Current Version FM12 Have a script that exports a few records to a PDF with a desired file name - works great - but I would like to add the current date to the file name...any help would be greatly appreciated! Tom On Fri, 17 Jan 2014 15:39:33 -0800 (PST), Tom E wrote: > Current Version FM12 > > Have a script that exports a few records to a PDF with a desired file name - works great - but I would like to add the current date to the file name...any help would be greatly appreciated! Compute a variable with the text you want and assign this variable as file name...

script to delete files of a certain wildcard-name that are older than the current day?
I'm trying to delete files that are older than the current day and are named with wildcard: *.flows* I created the cron job below that seems more complex than need be, but I can't get any other method to work. Has anyone done this before successfully and have a script I could use? Thanks!! Mike # Create reference files for -newer comparison 0 0 * * 0 touch /tcom/flows/sun 0 0 * * 1 touch /tcom/flows/mon 0 0 * * 2 touch /tcom/flows/tue 0 0 * * 3 touch /tcom/flows/wed 0 0 * * 4 touch /tcom/flows/thu 0 0 * * 5 touch /tcom/flows/fri 0 0 * * 6 touch /tcom/flows/sat # Delete files old...

append date to a file name using batch file.
I have a requirement where the current date (or even the file creation date) must be appeneded to a file name present in a specific folder. And how to rename the files if the folder have more than one file with different names. And also, How to handle renaming (appending the date) the files using a .bat file and Scheduling the .bat file using NT/2000 scheduler to run everyday. Thanks very Much. mercury wrote: >I have a requirement where the current date (or even the file creation >date) must be appeneded to a file name present in a specific folder. >And how to rename the file...

how to get file name from which current executing file is invoked???
hi how can i get the file name from which the current executing file is called. i m making a scrript which will be called from many other scripts. this script performs some calculation and then returns the result to the script who asked for. so i want to know the script name of caller script in the current executing script thxs for your help in advance. vishal <vishal_panjabi@yahoo.co.in> wrote: > how can i get the file name from which the current executing file is > called. Dump phpinfo(32) and you'll probably see the info you are looking for. > i m making a scrript w...

PHP script to get name of file which houses the script?
I have a number of files which contain PHP scripts like: <?php $file_name='mtg10.php'; include 'format_date.php'; ?> The included PHP script uses $file_name to get the last uploaded time stamp of the named file and process it into a displayable line. All that works as intended. What I don't like is the need for $file_name='mtg10.php'; It would be much handier if I could write something like $file_name=getNameOfThisFile(); instead. I've looked, can't find any such function, but maybe I haven't looked in the right references. Is the...