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

0
markryde (46)
1/29/2006 9:06:35 AM
comp.lang.python 74940 articles. 0 followers. Post Follow

2 Replies
326 Views

Similar Articles

[PageSpeed] 0
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
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
fredrik2101 (5275)
1/29/2006 9:27:16 AM
Reply:
Similar Artilces:

Adding histograms
I was able to write a program to give me a list of the count and grey levels of an image. (A histogram as a table). Now I would like to add the 'count's of the histograms of three images to each other and take the average. This is what I have so far: c = imread('lena.jpg'); [count, grays] = hist(c(:),0:255); save table.txt count grays -ascii; Any hints? I would also like to save each histogram into the same file, (table.txt), and have the file show the numbers as they show in the command window, as opposed to like this: 7.9900000e+002 5.4300000e+002. Help please. On...

New in V8 AD ?
What are the most important enhancement on the application development side in V8 ? BTW, we use Websphere-DB2 in our application Thanks -- Posted via http://dbforums.com ...

Re: How can I insert two macro variables name into one variable #5
Hi Jane, If things are as you say, you are correct. Macro variables created inside other macros are not available outside of those macros. Indeed they are local to those macros. Yes if you make them global %global myvar; in those macro routines, then the will be available outside of those macros. You can place the %GLOBAL statement for them inside or outside of those routines. e.g. %macro xxx; %global aaa; %let aaa=hello; %put inside macro is &aaa; %mend; %xxx; %put outside is &aaa; %global bbb; %macro yyy; %let bbb=hello; %put inside macro is &bbb; ...

Preview jgd-files (gradients?)
Is it possible to preview these files? I only want to install those gradients which are interesting to me. I�m using PSP8 and PSP9. Best wishes Linnea "Linnea" <linneanospam@mail.dk> wrote in message news:42234462$0$217$edfadb0f@dread11.news.tele.dk... > Is it possible to preview these files? > I only want to install those gradients which are interesting to me. > > I�m using PSP8 and PSP9. > > Best wishes > Linnea > There not images just a set of instructions in text form so there is nothing to preview. Linnea wrote: > Is it possible to p...

Python-URL!
QOTW: "XML. Almost as good as plain text for grepping." - Joe Mason "Where there's IP, there's a way." - Kyler Laird, on network programming Linked lists, deques, and iteration over a mutating container: http://groups.google.com/group/comp.lang.python/browse_thread/thread/1017de91323d9e23/ Containers should compare themselves using only their contained objects's available comparison operators: http://groups.google.com/group/comp.lang.python/browse_thread/thread/65bb28d2adf119d9/ The distinction between m...

Script to find files with resource forks?
Hi, all. I am currently using an out of date version of rsync, as Apple's new HFS-aware rsync borks the mod dates, and I have not gotten around to building a new one. The thought just struck me that I could use bog-standard rsync on any directory whose contents do not contain resource forks. Anyone have a find command or perl script that can list all files in a directory (recursively as far down as needed) with resource forks? I have tried various find commands, a couple of scripts, and the like, and have not yet had anything that could do this fairly simple sounding task. Scot...

Adding a Filesystem?
Hi, i'm in need of some help. I have added 2 18.3 Gb disks to my F50. I added both of the disks to a volumegroup called foto2. How do I add a JFS on this volumegroup so I can use it? Thanks, Rob Rob van der Heijden wrote: > Hi, i'm in need of some help. > > I have added 2 18.3 Gb disks to my F50. I added both of the disks to a > volumegroup called foto2. > How do I add a JFS on this volumegroup so I can use it? smit crjfs Thanks Simon, They are up and running. Regards, Rob "Simon Marchese" <sjm_news@yahoo.co.uk> schreef in bericht news:bno32j$3...

adding button to uigetfile
I'd like to add a button to a file picker screen that will change the directory to a library directory on our network. Since the uigetfile function seems to use java libraries extensively, I don't see an easy way to do this. Any ideas on the following options? It is a windows-only app, so activex etc are options. 1. File chooser written in .m so I can modify it - the ones on the file exchange do a lot more than I need - any simple ones out there? 2. Is there a way to modify the "places" buttons on the left side of the dialog box? I can do it for office applications...

Vision Evaluation License
Hello All; &nbsp; A word of caution if you are using LabVIEW 8.x with Vision 8.x installed as runtime: If you are using NI Vision in a runtime environment and have elected to use the 30-day evaluation license before purchasing a valid license, this is for you. &nbsp; The evaluation license version has a few quirks associated with it.&nbsp; This caused some minor grief for me, and so I am passing it along.&nbsp; The only way to resolve these issues (that I am aware of) is to purchase and activate a valid license. &nbsp; (1) Sometimes the license version will not always initi...

[OT] Adding ' ` ' key to keyboards
Yes, I know this is not too much verilog related... but perhaps I can save some time to other people. Verilog is ok, but in my keyboard, using all `define macros was a pain in the ass (missing "`" key). From Microsoft site you can donwload a wonderful utility that allows you to "design" your own keyboard as you prefer, starting from a predefined one. You can download here: http://tinyurl.com/4fymo Now I can type `define MUCH easily! Antonio Pasini wrote: > Yes, I know this is not too much verilog related... but perhaps I can save &...

trying to look at dates
Right now I had to build a report that allowed the people to check for gross outliers in their data input. short I am looking at 2.5* std dev + - anyway I used 2 dummy variables in the query the report is tied to. start date and end date. This pops up an input box for these values and they put in 11/01/08 and 11/31/08 and it runs the report showing the outliers. I watched and everyone is looking at month not date Now I was thinking instead fof this in the query under recDATE >=[start_date] And <=[end_date] is there a way of just asking for a month and year and then comparing the recDA...

File open history
Running 2001 build 2004290. At home Pro saves the "file open history" between sessions. In other words, if I close Pro, then open it a day later it still has the history of what I opened the day before. This only works on my home computer, not on the one at work. Does anyone know why this is? Thanks Mike Singer ...

output of DATE function
Hi, I am using DB v8.2. when I issuing the following stmt.: "select DATE('25.12.2006')-DATE('24.12.2006')from sysibm.sysdummy1" : 1 "select DATE('25.12.2006')-DATE('24.11.2006')from sysibm.sysdummy1" : 101 "select DATE('25.12.2006')-DATE('25.12.2005')from sysibm.sysdummy1" : 10000 "select DATE('25.12.2006')-DATE('01.12.2005')from sysibm.sysdummy1" : 10024 can anyone please explain the reason for this sort of output. thanks in advance....... satish In article <11641...

Adding objects to a live scenegraph
Hi, Im trying to add objects into my scene on some kind of MouseEvent. I want to click a certain point and have a cube for instance generated. The java3d tutorial from sun goes through basically every possible other use for every possibly event but doesnt seem to mention how you add to the scenegraph in this way!!! Any help would be greatly appreciated! Craig D wrote: > Hi, Im trying to add objects into my scene on some kind of MouseEvent. > I want to click a certain point and have a cube for instance generated. > The java3d tutorial from sun goes through basically every p...

script question #2
>From a script, I want to print all the lines of a file up to a line containing the word "END". Something like: # grep -avw -m 1 "END" -f bla.txt or # cat bla.txt | while read line; do echo $line if [ "$line" == "END" ]; then break; fi done ....except a script that actually works. The second one above does not work because the "read line" corrupts the lines by removing whitespace. I don't know why the first one doesn't work. -Mike In <1174683868.851106.205880@e65g2000hsc.googlegroups.com> "Mik...

Read raw binary file
I've been playing around with reading binary data into a testbench. I noticed a lot of people asking questions on the user group over the years, but didn't see a simple expample, so here's some code that might help: constant N : integer := 28671; --Number of bytes in file minus one subtype file_element is std_logic_vector(7 downto 0); type mem_array is array(N downto 0) of file_element; shared variable memory : mem_array; begin ---------------------------------------------------------------------------- -- This code reads a raw binary file one byte at a time. load...

Adding custom kernel IOCTL
Hi. I try to read MSR from GPIO driver. As I understand, I can't use NKrdmsr function outside the OAL. So I have to implement the custom kernel IOCTL. How do I add this IOCTL to my project? P.S. I work with Windows CE 5. Thank you in advance. Leon ...

Adding HD to G4
I have a G4/400 and would like to add an additional HD. I am loath to do it myself so am considering a local guy to do the installation. Questions: 1) suggestions for say a 80 or 100 GB HD, I don't need great speed and 2) what is a decent charge for installation? Michael In article <mipaine-2134BF.14270009102004@comcast.dca.giganews.com>, "G. Michael Paine" <mipaine@comcast.net> wrote: > Questions: 1) suggestions for say a 80 or 100 GB HD, I don't need great > speed and 2) what is a decent charge for installation? Price differences for drives in the ...

Getting first 2 numbers of the current year
How do I parse out the 20 from the current year (2005). I can get the last 2 numbers on their own, but have been unable to get the first two on their own. TIA dixie I have found that this works. Is it OK? =Mid(Date(),7,2) NB Australian dates. dixie "Dixie" <dixie@dogmail.com> wrote in message news:435c088c$1@duster.adelaide.on.net... > How do I parse out the 20 from the current year (2005). I can get the > last 2 numbers on their own, but have been unable to get the first two on > their own. > > TIA > dixie > Dixie wrote: > I have fou...

problem in adding disman-ping-mib
hi, I want to add disman-ping-mib in net-snmp 5.2 package, its code is there in with agent(netsnmp/agent/disman/ping) directory but it is not build with package. I made changes in agent makefile to add ping-mib as it is doing for other mib like if-mib and i got my new snmpd agent, but when I am Quering for ping-mib from mager command (like snmpwalk, snmoset,snmpget) it is not responding. 1. Can anyone tell me what changes exactly i need to do for adding disman-ping-mib in net-snmpd agent ? 2. Is there any configuration changes requires on mager side for quering to ping-mib(I...

Lst output to excel file
Hi All, Any idea how to send the lst file output to xl file. I have used the following option, but all the values are coming under one column in xl. proc printto file='c:\test.xls' new; run; Thanks in advance. Regards, Raja Raja, There may be a more direct solution, but you could always use Excel's data>text-to-columns feature to disect the column. Of course, you may have to first strip out titles that don't match the fixed length format. An example is shown, I believe, in the on-line printto documentation. Art -------- "Raja" <absimip@yahoo.co.in> ...

Disable ADS
Heya all, As an admin, how can I stop people from hiding pr0n in ADS's? ...

Long folder name problem with LanMan98
Although I do not make a habit of having very long directory names, I do make use of the ability to have longer, therefore more meaningful names. I have a folder on Vista PC with a 25 character long name which I want to share with my Iyonix via LM98 v.2. LM98's Share field only allows a max of 20 characters, so of course the name is not recognised. Before I do the obvious, and rename the PC folder at least 5 characters shorter, is there any other way, please? If there was it would just allow me to keep my PC naming system as it is now, that's all. -- Alex. In...

Adding ProxyPass
Currently running Apache Server 2.0.58 on Solaris 5.9 with NewAtlanta ServletExec 5.0 supporting an ARS (Remedy) System Mid-Tier Version 7.0.1 Patch02 with no issues. Trying to connect an Knowledge Management application on a Windows 2003 Server. They asked me to add this > ProxyPass /usr/ar/kms/ http://servername/kms/ to the httpd.conf. As you can tell I'm not familiar with Apache but I did the installs for the mid-tier. When I ran >apachectl configtest Syntax error on line 1082 of /usr/local/apache2.0.58/conf/httpd.conf: Invalid command 'ProxyPass', perha...

rdist and file owners #2
Hi All, need some help with a problem using rdist.. I've server A and server B. The last admin setup a cron job so that rdist was used to copy the home areas from A to B, for backup. Last week, server As raid died, and I switched NIS over to using server B. But I found that files on B had file ownerships of "root" instead of the users own. I was able to fix it with a short script and a global replace, but now I need to solve it. I've fixed the raid, and I'm getting ready to use rdist to copy the changes back - but when I use rdist, I find that copied files get set ...