1

Been researching this for a while now and can't seem to find a solution. I'm starting to think that there may not be one. Note, I am still self teaching myself Python, so I may be missing an easy solution.

Here's the line:

time = time.strftime("%Y%m%d-%H%M%S")
 sys.stdout = open('/home/username/logfilesgohere/%s', % time 'w')

I'm trying to make the file that stdout writes to reflect the current timestamp as per the time variable. This would be easy to do in bash with backticks but I'm a python newb. Also to note that I'm using this in my fabfile for fabric.

1
  • There shouldnt be comma before %, and there should be one before 'w'. sys.stdout = open('/home/username/logfilesgohere/%s'% time,'w') Commented Apr 19, 2013 at 0:01

1 Answer 1

4

You simply put the comma in the wrong place. This is what you want:

sys.stdout = open('/home/username/logfilesgohere/%s' % time, 'w')

This piece of code

'/home/username/logfilesgohere/%s' % time

will build the full filename string.

Sign up to request clarification or add additional context in comments.

1 Comment

Also you might want to avoid using time as name since it is the name of the time module as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.