Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 用Python批处理文件重命名_Python 3.x_Os.path - Fatal编程技术网

Python 3.x 用Python批处理文件重命名

Python 3.x 用Python批处理文件重命名,python-3.x,os.path,Python 3.x,Os.path,下面是我在给定目录中批量重命名图片的代码 def multi_filename_change(): i = 0 files = askstring('Select your folder', 'Paste your directory path where your images are stored.') for file in os.listdir(files): if not file.startswith('.'): file_name = askstring('

下面是我在给定目录中批量重命名图片的代码

def multi_filename_change():
i = 0
files = askstring('Select your folder', 'Paste your directory path where your images are stored.')
for file in os.listdir(files):
    if not file.startswith('.'):
        file_name = askstring('Add file name', 'Please enter a name for your files.')
        src = file
        dst = file_name + str(i) + ".jpg"
        os.rename(src, dst)
        i += 1
运行此操作时,我会收到以下错误消息:

os.rename(src, dst) FileNotFoundError: [Errno 2] No such file or directory: '360007_space-wallpaper-4k.jpg' -> 'test0.jpg'
我似乎无法解决这个问题,对于各位专家来说,这可能是一个简单的问题:


谢谢

源文件应附加现有目录,而不仅仅是文件名

src=文件+文件


src=os.path.joinfiles,file

os.listdirfiles中文件中的文件不包括路径名文件。如果您打印出文件,您可能已经看到了。如何包括文件的路径?文件是您遍历并重命名图像的路径,因此您可以将src=file更改为src=files+file。dst也是如此。这工作非常完美,我现在唯一的问题是重命名后的图像会转到我的python脚本目录,并且它们会从原来所在的目录中移动?!这是因为我们也没有更新dst,它正在写入当前工作目录,更改dst=files+file_name+stri+.jpg。我将dst更改为:dst=files+/+file_name+stri+.jpg,现在它可以工作了,感谢您的帮助!