Python重命名文件从csv文件读取名称 [英] Python rename files reading names from csv file

查看:2061
本文介绍了Python重命名文件从csv文件读取名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我一直在尝试适应对我的需要,但我只是一个新的python,我有一个csv文件与多列和行,重要的列是1 =旧的文件名称,和2 =文件的新名称,所以我需要去到csv文件中列出的文件所在的目录,并将它们重命名为列2的新名称,因为我说我试过很多事情没有成功,我粘贴了最后一个代码,所以你有一个想法:

  import os,unicodecsv as csv,sys 

IDs = {}

#open并将csv文件
与open('documentos_corpus_ladino.csv','rb')一起存储为csvfile:
timeReader = csv.reader(csvfile,delimiter =',')

#构建一个带有相关ID的字典
for rowReader:
IDs [row [0]] = row [1]

#文件列表
path ='txt_orig /'
tmpPath ='txt_tmp /'
在os.listdir('txt_orig /')中的文件名:
oldname = filename
newname = filename.replace(oldname,csvfile.next()。rstrip()。split(,)[1]
os.rename(path + filename,tmpPath + newname)



非常感谢。

解决方案

这将重命名每个匹配的文件,并报告尝试重命名的任何错误。它不会尝试移动不存在的文件。

  import os,unicodecsv as csv 
#打开并存储csv文件
IDs = {}
with open('documentos_corpus_ladino.csv','rb')as csvfile:
timeReader = csv.reader(csvfile,delimiter =',')
#构建具有相关标识的字典
for timeReader:
IDs [row [0]] = row [1]
#移动文件
path ='txt_orig /'
tmpPath ='txt_tmp /'
对于os.listdir(路径)中的oldname:
#忽略路径中不在csv文件中的文件
如果ID中的oldname:
try:
os.rename(os.path.join(path,oldname),os.path.join(tmpPath,IDs [oldname]))
except:
print 'file'+ oldname +'无法重命名为'+ IDs [oldname] +'!'


Hi there i've been trying to adapt this to my needs but I'm just a newbe in python, I have a csv file with multiple columns and rows, important columns are 1 = old name of file, and 2 = new name of file, so I need to go the directory where the files listed in csv file are and rename them to the new name of column 2, as I say I've tried many things without success, I paste the last code I've made so you have an idea:

import os, unicodecsv as csv, sys

IDs = {}

#open and store the csv file
with open('documentos_corpus_ladino.csv','rb') as csvfile:
        timeReader = csv.reader(csvfile, delimiter = ',')

        # build a dictionary with the associated IDs
        for row in timeReader:
              IDs[ row[0] ] = row[1]

# #get the list of files
path = 'txt_orig/'
tmpPath = 'txt_tmp/'
for filename in os.listdir('txt_orig/'):
    oldname = filename
    newname = filename.replace(oldname, csvfile.next().rstrip().split(",")[1])
    os.rename(path + filename, tmpPath + newname)

Thanks a lot.

解决方案

This will rename each matching file, and report any errors trying to rename. It will not attempt to move non-existent files.

import os, unicodecsv as csv
# open and store the csv file
IDs = {}
with open('documentos_corpus_ladino.csv','rb') as csvfile:
    timeReader = csv.reader(csvfile, delimiter = ',')
    # build dictionary with associated IDs
    for row in timeReader:
        IDs[row[0]] = row[1]
# move files
path = 'txt_orig/'
tmpPath = 'txt_tmp/'
for oldname in os.listdir(path):
    # ignore files in path which aren't in the csv file
    if oldname in IDs:
        try:
            os.rename(os.path.join(path, oldname), os.path.join(tmpPath, IDs[oldname]))
        except:
            print 'File ' + oldname + ' could not be renamed to ' + IDs[oldname] + '!'

这篇关于Python重命名文件从csv文件读取名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆