文件名我有:散装搜索/替换使用python
- Excel文件为A1:B2。
- 一个包含200个jpeg文件的文件夹。
我试图与价值搜索文件名的文件夹中Column A
如果发现不改变文件的扩展名的文件夹中,在Column B
值替换它。
在这里,我坚持使用各种打滑来做到这一点,但失败了。这里是我的代码:
import os
import xlrd
path = r'c:\users\c_thv\desktop\x.xls'
#collect the files in fexceler
path1 = r'c:\users\c_thv\desktop'
data = []
for name in os.listdir(path1):
if os.path.isfile(os.path.join(path1, name)):
fileName, fileExtension = os.path.splitext(name)
if fileExtension == '.py':
data.append(fileName)
#print data
#collect the filenames for changing
book = xlrd.open_workbook(path)
sheet = book.sheet_by_index(0)
cell = sheet.cell(0,0)
cells = sheet.row_slice(rowx=0,start_colx=0,end_colx=2)
excel = []
#collect the workable data in an list
for cell in cells:
excel.append(cell)
#print excel
#compare list for matches
for i,j in enumerate(excel):
if j in data[:]:
os.rename(excel[i],data[i])
它怎么会失败呢?什么(没有)发生? – 2014-10-28 21:01:56
@TimCastelijns: - 没有任何更改。 。没有效果。 。我认为比较两件事情(名单)出了问题。我对**重命名**行不太确定。如果我错了,请纠正我。 – Thuruv 2014-10-28 21:23:08
@TimCastelijns我可以使用字典。 。? 任何方法都可以创建出色的字典。 。? – Thuruv 2014-10-30 00:32:49