python - 如何更改 numpy 文件列表的名称?

标签 python numpy

我有 numbpy 文件列表,我需要更改它们的名称,事实上,我们假设我有这个文件列表:

AES_Trace=1_key=hexaNumber_Plaintext=hexaNumber_Ciphertext=hexaNumber.npy
AES_Trace=2_key=hexaNumber_Plaintext=hexaNumber_Ciphertext=hexaNumber.npy
AES_Trace=3_key=hexaNumber_Plaintext=hexaNumber_Ciphertext=hexaNumber.npy

我需要更改的是文件数量,因此我必须具有:

AES_Trace=100001_key=hexaNumber_Plaintext=hexaNumber_Ciphertext=hexaNumber.npy
AES_Trace=100002_key=hexaNumber_Plaintext=hexaNumber_Ciphertext=hexaNumber.npy
AES_Trace=100003_key=hexaNumber_Plaintext=hexaNumber_Ciphertext=hexaNumber.npy

我已经尝试过:

import os
import numpy as np
import struct
path_For_Numpy_Files='C:\\Users\\user\\My_Test_Traces\\1000_Traces_npy'
os.chdir(path_For_Numpy_Files)
list_files_Without_Sort=os.listdir(os.getcwd())
list_files_Sorted=sorted((list_files_Without_Sort),key=os.path.getmtime)
for file in list_files_Sorted:
    print (file)
    os.rename(file,file[11]+100000)

我认为这不是一个好的解决方案,首先它不起作用,然后它给了我这个错误:

os.rename(file,file[11]+100000)
IndexError: string index out of range

最佳答案

您的 file 变量是 str,因此您无法向其中添加 int(如 10000) .

>>> file = 'Tracenumber=01_Pltx5=23.npy'
>>> '{}=1000{}'.format(file.split('=')[0],file.split('=')[1:])
'Tracenumber=100001_Pltx5=23.npy'

所以,你可以使用

os.rename(file,'{}=1000{}'.format(file.split('=')[0],file.split('=')[1:]))

关于python - 如何更改 numpy 文件列表的名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42948792/

相关文章:

python - 使用 Parsimonious Python 库解析多行文本

python - 色相色彩空间中的图像分割

python - 将复杂的 numpy dtypes(即由元组数组组成的 dtypes)解析为列表?

python - 在图像中找到最少数量的矩形

python - 从 numpy 中的较小矩阵创建较大矩阵

python - 有没有办法检查符号是否是 Python 中的有效运算符?

python - 如何将 MYSQL Latin-1 GEOMETRY 字段转换为 UTF8 以使其与 Django 一起使用?

python - 向下循环鼠标按钮以绘制线条

python - 如何使用 open() 函数清除 .txt 文件?

python - 运行 scipy.test ('full' 需要多长时间)?