【问题标题】:How to modify the name of a file? [duplicate]如何修改文件名? [复制]
【发布时间】:2017-04-25 22:18:20
【问题描述】:

假设这是我的 numpy 文件的名称:

 File1.npy
 File2.npy

我需要修改文件名,添加一条重要信息:

File1_SV1. npy 
File2_SV2

我尝试使用这行代码:

path ='C:\\Users\\mky\\Folder'
Files= os.listdir(path)
for File in Files:
      SV= # I must calculate the SV
      os.rename(os.path.join(path,File), os.path.join(path, File+SV))

它给了我这个错误:

os.rename(os.path.join(path,File), os.path.join(path, File+SV))
TypeError: must be str, not int

【问题讨论】:

  • SV转换为字符串
  • 可能SV 是一个整数而不是字符串。您可以通过使用 str() 文字将其转换为字符串。即str(SV).

标签: python numpy


【解决方案1】:
import os
f1 = File.replace('.npy',"")
os.rename(os.path.join(path,File), os.path.join(path, f1+str(SV)+".npy"))

【讨论】:

  • 虽然欢迎使用此代码 sn-p,并且可能会提供一些帮助,但它会是 greatly improved if it included an explanation of 如何解决这个问题。没有这个,你的回答就没有多少教育价值了——记住你是在为未来的读者回答这个问题,而不仅仅是现在提问的人!请edit您的答案添加解释,并说明适用的限制和假设。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-14
  • 2016-07-11
  • 2016-05-24
  • 2014-04-17
相关资源
最近更新 更多