Reputation: 41
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
Upvotes: 0
Views: 1513
Reputation: 128
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))
Upvotes: 1
Reputation: 5665
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
Upvotes: 1