简体   繁体   English

为什么我无法使用os.listdir更改Python程序中文件的名称

[英]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. 我开始使用Raspberry Pi 3B +和佳能6D创建一个新的3D扫描仪。 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. 由于有了gphoto2库,我有一部分Python代码来恢复图像,但是我无法更改恢复的图像的名称,目前,我有两个文件:capt0000.cr2和capt0000.jpg我必须将它们重命名为“ time” + .jpg或.cr2,但不可能,他们从不更改名称。

I tried several methods and currently I am with the os.listdir function that allows me to sort all the files on the desktop. 我尝试了几种方法,目前使用的是os.listdir函数,该函数使我可以对桌面上的所有文件进行排序。

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 现在,我在桌面上创建了两个文件,请参见下图: https : //i.imgur.com/DDhYe1L

Is it due to file permissions knowing that I am not the root user? 是由于文件权限知道我不是root用户吗? 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://imgur.com/VydSeAH 如果是因为这样,通常如何更改文件类型的权限,例如.jpg,因为它每次都是关于新图像的,因此该权限返回以下图像: https : //imgur.com/VydSeAH

I guess it's a Problem with os.chdir(save_location) . 我想这是os.chdir(save_location) You got to use the complete path (see https://www.tutorialspoint.com/python/os_chdir.htm ) Try something like 您必须使用完整路径(请参阅https://www.tutorialspoint.com/python/os_chdir.htm )尝试类似

path = os.path.join(os.getcwd(), save_location)
os.chdir(path)

If you want to change file permissions in your code, use os.getcwd() (see https://www.tutorialspoint.com/python/os_chown.htm ). 如果要更改代码中的文件权限,请使用os.getcwd() (请参阅https://www.tutorialspoint.com/python/os_chown.htm )。 You can get your current UID by os.getuid() . 您可以通过os.getuid()获得当前的UID。 So add to renameFiles : 所以添加到renameFiles

uid = os.getuid()
gid = os.getgid()
for filename in os.listdir("."):
    filepath = os.path.join(os.getcwd(), filename)
    os.getcwd(filepath, uid, gid)
    ....

so all the files will belong to the current user. 因此所有文件都将属于当前用户。 Maybe you need to run your script with "sudo" 也许您需要使用“ sudo”运行脚本

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. 您只需要在创建图像文件夹之前重命名文件即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM