bubbah12
Posts: 33
Joined: Fri Jul 01, 2016 9:24 am

variables to file name

Wed Jul 13, 2016 10:01 am

Hi,

This is probably a very simple question, but I cannot work it out.

In Python, I want to define a variable as image1-%H%M%S

variable = image1-%H%M%S (date/time/min/sec)

This is fine, but I cannot work out how I can put the variable value into a filename when saving a jpeg
i.e

filename images_folder/(variable).jpg

Could someone help me?
Thanks in advance.
Phil

pcmanbob
Posts: 9612
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: variables to file name

Wed Jul 13, 2016 11:53 am

Hi Phil

To create your file name string you have to first convert your date/time in to a string using (time.strftime("%b-%d-%Y-%H-%M-%S"))
then you can assemble your folder and file name string.

I have created an example to show how you could do this, it creates a text file in the /home/pi directory.
you can then use this example to create your own string, remembering that only certain special characters are allowed in file names.

Code: Select all

import time

variable = (time.strftime("%b-%d-%Y-%H-%M-%S"))
filename = "/home/pi/Image1-" + variable + ".txt"
print filename
result = open(filename,"a")
result.write(" test \n")
result.close()
hope this helps
Rob
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

bubbah12
Posts: 33
Joined: Fri Jul 01, 2016 9:24 am

Re: variables to file name

Wed Jul 13, 2016 3:27 pm

Thank you for that. But if I wanted to place the variable in the line below, how would it be constructed?


graphicsmagick = "cp /home/pi/temp_montage3.jpg (Variable_name).jpg"


I also wanted it to go into an ftp line:

with open('(variable name)', 'r') as f:
ftp.storlines('STOR %s' % '(variable name)', f)
ftp.quit()


Sorry for sounding so vague. I'm learning as I go...Or trying to:)

User avatar
Paeryn
Posts: 2986
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: variables to file name

Wed Jul 13, 2016 7:45 pm

bubbah12 wrote:Thank you for that. But if I wanted to place the variable in the line below, how would it be constructed?

graphicsmagick = "cp /home/pi/temp_montage3.jpg (Variable_name).jpg"
Create your filename as pcmanbob said above, modify the time string according to how you want it formatted - I've given it in the order that will appear oldest first when listed alphabetically (numerically) - year-month-day-hour-minute-second. This will give you a filename similar to image1-2016-07-13-20-33-12.jpg

Code: Select all

filename = "image1-%s.jpg" % time.strftime("%Y-%m-%d-%H-%M-%S")
graphicsmagick = "cp /home/pi/temp_montage3.jpg %s" % filename
bubbah12 wrote: I also wanted it to go into an ftp line:

with open('(variable name)', 'r') as f:
ftp.storlines('STOR %s' % '(variable name)', f)
ftp.quit()


Sorry for sounding so vague. I'm learning as I go...Or trying to:)
Create the string in the same way here - your version is almost there. Although if you're ftping an image you may want to use ftp.storbinary() rather than ftp.storlines() since the data isn't ASCII (and open the file with 'rb').

Code: Select all

with open(filename, 'rb') as f:  
    ftp.storbinary('STOR %s' % filename, f)
ftp.quit()
She who travels light — forgot something.

bubbah12
Posts: 33
Joined: Fri Jul 01, 2016 9:24 am

Re: variables to file name

Wed Jul 13, 2016 8:35 pm

Thanks...i think i follow...I'll give it a try....thanks for everybody's help

Return to “Python”