0

I used the following line to rename my file by adding timing and remove extra space and replace it with (-) if i would like to add extra information like lable before the timing ,

filename = ("%s_%s.mp4" %(pfile, time.strftime("%Y-%m-%d_%H:%M:%S",time.localtime()))).replace(" ", "-")

the current output looks like

testfile_2016-07-25_12:17:14.mp4

im looking to have the file output as

testfile_2016-07-25_12:17:14-MediaFile.mp4

try the following ,

filename = ("%s_%s_%s.mp4" %(pfile, time.strftime("%Y-%m-%d_%H:%M:%S","Mediafile",time.localtime()))).replace(" ", "-")

what did i missed here ?

1
  • filename = ("%s_%s_%s.mp4" %(pfile, time.strftime("%Y-%m-%d_%H:%M:%S",time.localtime()),"Mediafile")).replace(" ", "-") should work : the string 'MediaFile' was not in the right parentheses. Commented Jul 25, 2016 at 8:53

3 Answers 3

2

You're using the function strftime incorrectly. Strftime only takes 2 arguments and you're passing it 3.

You would need to generate the string from the time and apply some string operations to append the extra info.

If you want to add MediaFile to the end of the filename simply do something like this.

filename = ("%s_%s-MediaFile.mp4" %(pfile, time.strftime("%Y-%m-%d_%H:%M:%S",time.localtime()))).replace(" ", "-")
Sign up to request clarification or add additional context in comments.

Comments

1
filename = ("%s_%s-%s.mp4" %(pfile, time.strftime("%Y-%m-%d_%H:%M:%S",time.localtime()), 'MediaFile')).replace(' ', '-')
# 'testfile_2016-07-25_10:29:28-MediaFile.mp4'

To understand better how this works and slightly improve readability, you can define your time stamp in a separate variable:

timestr = time.strftime("%Y-%m-%d_%H:%M:%S", time.localtime()) # 2016-07-25_10:31:03

filename = ("%s_%s-%s" %(pfile, timestr, 'MediaFile')).replace(' ', '-')
# 'testfile_2016-07-25_10:31:03-MediaFile.mp4'

or

filename = ("%s_%s-MediaFile.mp4" %(pfile, timestr)).replace(' ', '-')

For completeness, you can also use the format() method:

filename = '{0}_{1}-MediaFile.mp4'.format(pfile, timestr).replace(' ', '-')

2 Comments

I think he wants to replace the space for '-' in case there are whitespaces in the filename.
@HolyDanna Fair enough, i'll take it out
0

What you are looking for should be :

filename = ("%s_%s_%s.mp4" %(pfile, time.strftime("%Y-%m-%d_%H:%M:%S",time.localtime()),"Mediafile")).replace(" ", "-")

In your original code, the 'Mediafile' string was not in the right place : you put it as an argument of strftime(), when you should put it as one of the string to replace, in the 2nd level of parentheses.

Comments

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.