0

I am trying to write code in python to rename a file by using NAME_COMPUTERNAME_ and then the current date.

I have the following code:

PCname=socket.gethostname()
date_now = datetime.now().strftime'%d%m%Y')

file_one = "C:\\Reports\\Report.pdf"

file_two = 'C:\\Reports\\VSR_(PCname)_{}.pdf'.format(date_now)

os.rename(file_one, file_two)

I can rename the file with just VSR_14092018.pdf but I would like to include the computername also after VSR in the file's name.

Thanks

3 Answers 3

1

Replace file_two with below code

from datetime import datetime
date_now=datetime.strftime(datetime.now(),'%d%m%Y')
PCname='my_host'
file_two = 'C:\\Reports\\VSR_{}_{}.pdf'.format(PCname,date_now)
Sign up to request clarification or add additional context in comments.

Comments

0

In your code, it does not recognize PCname (also note that you should write variable names lower case) as a the variable from above. You can try this:

file_two = r'C:\\Reports\\VSR_({})_{}.pdf'.format(PCname, date_now)

1 Comment

I will do it I need to wait 5 min before I can do it. Sorry I am new at python so most likely I will have stupid questions.
0
import os
import socket
date = datetime.datetime.now().strftime("%d%m%Y")
file_one = "C:\Reports\Report.pdf"
file_two = 'C:\\Reports\\VSR_{}_{}.pdf'.format(socket.gethostname(),date_now)
os.rename(file_one, file_two)

in file_two variable you need to pass the hostname like above.

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.