pythonpython-3.xraspberry-pidebianlibgphoto2

Why I can't change the name of my files in my Python program using os.listdir


I'm starting to create a new 3D scanner with a Raspberry Pi 3B + and a Canon 6D. I have a part of the Python code to recover the images thanks to the gphoto2 library but I can not change the name of the recovered images, currently, I have two files: capt0000.cr2 and capt0000.jpg I must renames them to "time" + .jpg or .cr2 but impossible, they never change their name.

I tried several methods and currently I am with the os.listdir function that allows me to sort all the files on the desktop.

Program start:

from time import sleep
from datetime import datetime
from sh import gphoto2 as gp
import signal, os, subprocess

shot_date = datetime.now().strftime("%d-%m-%Y")
shot_time = datetime.now().strftime("%d-%m-%Y %H:%M:%S")
picID = "PiShots"
folder_name = shot_date + picID
save_location = "ScannerImages/" + folder_name

CaptureImageDownload = ["--capture-image-and-download"]
CaptureImage = ["--capture-image"]

Functions:

def captureImageDownload():
    gp(CaptureImageDownload)

def captureImage():
    gp(CaptureImage)

def createFolder():
    try:
        os.makedirs(save_location)
    except:
        print("Failed to create folder")
    os.chdir(save_location)

def renameFiles(ID):
    for filename in os.listdir("."):
        if len(filename) < 13:
            if filename.endswith(".jpg"):
                os.rename(filename, (shot_time + ID + ".jpg"))
            print("Renamed the JPG")
        elif filename.endswith(".cr2"):
            os.rename(filename, (shot_time + ID + ".cr2"))
            print("Renamed the CR2")

Main loop:

captureImageDownload()
createFolder()
renameFiles(ID)

Now I have two files that are created on the desktop see image below: https://i.imgur.com/DDhYe1L

Is it due to file permissions knowing that I am not the root user? If it is because of that how to change the permissions on a type of file in general for example .jpg because each time it is about a new image so the permissions return to the image below: https://i.sstatic.net/okfSx.jpg


Solution

  • The problem is solved, here is the solution:

    Main loop:

    captureImageDownload()
    renameFiles(ID)
    createFolder()
    

    You just had to rename the file before creating the image folder.