Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.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 我希望包含y.png文件的x文件夹的名称为z.png_Python - Fatal编程技术网

Python 我希望包含y.png文件的x文件夹的名称为z.png

Python 我希望包含y.png文件的x文件夹的名称为z.png,python,Python,你会发现我的代码我不是PythonPro,但我做了一切来成为它 import os, sys os.listdir(os.getcwd()) Out[3]: ['.ipynb_checkpoints', 'helmet_10_0.png', 'helmet_10_1.png', 'helmet_10_10.png', I wish Out[0]: ['casque_Chantier_10_0.png', 'casqu

你会发现我的代码我不是PythonPro,但我做了一切来成为它

import os, sys
os.listdir(os.getcwd())


Out[3]: ['.ipynb_checkpoints',
         'helmet_10_0.png',
         'helmet_10_1.png',
         'helmet_10_10.png',

I wish

Out[0]: ['casque_Chantier_10_0.png',
         'casque_Chantier_10_10.png',
         'casque_Chantier_10_100.png'
诸如此类

我不能循环使用“glob”并重命名所有文件

<ipython-input-33-1a271eebe4a0> in <module>
      1 for i in source:
      2     if i != dest:
----> 3         os.rename(i,dest)

FileNotFoundError: [WinError 2] Le fichier spécifié est introuvable: 'D' -> 'D:\\Chasse_Au_tressor\\base_agmt\\extractedFrames_step_5\\Casque_Chantier_{*}.png'

in
1对于源中的i:
2如果我目的地:
---->3.重命名操作系统(i,dest)
FileNotFoundError:[WinError 2]Le fichier spécifiéest introuvable:'D'->'D:\\Chasse\u Au\u tressor\\base\u agmt\\extractedFrames\u step\u 5\\Casque\u Chantier\u{*}.png'

我想要casque_Chantier_{index=1}.png上的图像,下面的代码遍历每个文件,并根据需要重命名该文件。它将“头盔”部分替换为“头盔”。 我已将其设置为与要重命名的文件在同一文件夹中运行

import os

for file in os.listdir():
    os.rename(file, file.replace("helmet_","casque_Chantier_")

下面的代码遍历每个文件,并根据需要重命名该文件。它将“头盔”部分替换为“头盔”。 我已将其设置为与要重命名的文件在同一文件夹中运行

import os

for file in os.listdir():
    os.rename(file, file.replace("helmet_","casque_Chantier_")

试试这个。您只需重命名文件名,而不必打开它并再次重命名它,这将在for循环中对多个文件执行操作时节省内存。这也将确保仅重命名预期文件,即仅以“头盔”开头的文件名

#Import package
import os

#Loop through the file names
for filename in os.listdir(os.getcwd()):
    if filename.startswith("helmet_"):
        os.rename(filename, "casque_Chantier_"+filename[7:])

干杯

试试这个。您只需重命名文件名,而不必打开它并再次重命名它,这将在for循环中对多个文件执行操作时节省内存。这也将确保仅重命名预期文件,即仅以“头盔”开头的文件名

#Import package
import os

#Loop through the file names
for filename in os.listdir(os.getcwd()):
    if filename.startswith("helmet_"):
        os.rename(filename, "casque_Chantier_"+filename[7:])
import os, sys

directory = os.getcwd()
filenames = os.listdir(directory)

renamed_filenames = [f.replace('helmet', 'casque_Chantier') for f in filenames]

for i in range(len(filenames)):

    source_filepath = os.path.join(directory, filenames[i])
    dest_filepath = os.path.join(directory, renamed_filenames[i])

    os.rename(source_filepath, dest_filepath)

干杯

以下内容适用于python 3.6

import os, sys

directory = os.getcwd()
filenames = os.listdir(directory)

renamed_filenames = [f.replace('helmet', 'casque_Chantier') for f in filenames]

for i in range(len(filenames)):

    source_filepath = os.path.join(directory, filenames[i])
    dest_filepath = os.path.join(directory, renamed_filenames[i])

    os.rename(source_filepath, dest_filepath)
您将需要从字符串库中替换以将“头盔”更改为“casque_Chantier”:

import shutil
import os
dir_path = os.getcwd()

for filename in os.listdir(dir_path):
    src = os.path.join(dir_path, filename)
    dst = os.path.join(dir_path, filename.replace("helmet", "casque_Chantier"))
    shutil.move(src, dst)

以下内容适用于Python3.6

您将需要从字符串库中替换以将“头盔”更改为“casque_Chantier”:

import shutil
import os
dir_path = os.getcwd()

for filename in os.listdir(dir_path):
    src = os.path.join(dir_path, filename)
    dst = os.path.join(dir_path, filename.replace("helmet", "casque_Chantier"))
    shutil.move(src, dst)

非常感谢你非常真实我变得更喜欢python非常真实我变得更喜欢python