0

I am trying to change the name of file using Python but the name does not change.

The program has to take a photo, save it, copy it to a server, change the name of copied photo and change the name of original photo. So, first of all I define a variable which is going to be a exact date and time. After that, I take a photo and save it like imagen.jpg Following, I copy imagen.jpg to other path (server path), and try change its name to the variable which I defined before. But the name does not change and the file is removed.

After that I change the name of original photo with the same function and the name is changed....I don't know why the copied photo doesn't change its name.

Here's the code:

import shutil
import picamera
import os
fecha = time.strftime("%c")  # En esta variable se guarda la fecha actual y la hora para renombrar la foto guardada          
camera.capture('/home/pi/Desktop/RaspAlarm/imagen.jpg')
print("Capturando foto")
time.sleep(5)
print("Copiando foto al servidor")
shutil.copy("/home/pi/Desktop/RaspAlarm/imagen.jpg", "/var/www/html/RaspAlarm/Fotos")
time.sleep(1)
os.listdir("/var/www/html/RaspAlarm/Fotos")
os.rename ("/var/www/html/RaspAlarm/Fotos/imagen.jpg", fecha)
print("Cambiando nombre al archivo")
os.rename ("/home/pi/Desktop/RaspAlarm/imagen.jpg", fecha)
time.sleep(1)
print("Foto guardada")

Could you help me? Thank you

2 Answers 2

1

Your destination name is wrong. You are basically calling:

os.rename("/var/www/html/RaspAlarm/Fotos/imagen.jpg", "Mon May  8 16:15:07 2017")

while what you want looks more like

os.rename("/var/www/html/RaspAlarm/Fotos/imagen.jpg", "/var/www/html/RaspAlarm/Fotos/Mon May  8 16:15:07 2017.jpg")

Check the documentation for os.rename

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

3 Comments

Yes I know. But when I try to hange the name of original photo the name is changed successfully using the same function. Why? Or, how can I add the .jpg extension like you said before?
@SergioMuñoz to add an extension you can use os.path.splitext or just path + fecha + '.jpg' if you're dealing with jpegs only. And yes, check out the os.path library.
Ok! I will try this. But is rare that in the original photo the name is changed well
1

At first your destination is wrong! you should write all the path. Then you should remove spaces in fecha variable :

fecha = fecha.replace(' ', '')
os.rename("/var/www/html/RaspAlarm/Fotos/imagen.jpg", "/var/www/html/RaspAlarm/Fotos/{}.jpg".format(fecha))

2 Comments

Thank you for you answer. But Why the original photo changes his names well? If I am using the same function
I dont think so, os.remove work exactly like mv command in Linux if you put destination as a word not as a path it will move the file to the function's directory. So I think maybe it works for you because the function is in the same directory as the original photo!

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.