我有一个包含具有多个扩展名的文件的目录,但我只对使用特定的文件感兴趣。将更改应用于同一目录中的多个文件
- document.doc
- file_with_the.extensionwanted
- other_file.extensionwanted
- presentation.ppt
- sheet.xls
- whatever.extensionwanted
这些文件,我想与以下格式一起使用csv风格:
This is a sentence, Info 1, Info 2, Info 3,...
This is a number: 37, Info 1, Info 2, Info 3,...
This is a letter: r, Info 2, Info 3,...
This is a symbol: $, Info 1, Info 2, Info 3,...
Here theres 'mb' too, Info 1, Info 2, Info 3,...
我希望运行删除与.extensionwanted包含在第一列中有两个不同的字符串结尾的文件,每行一个脚本,并有结果与相同的扩展名,避免空行的文件(我只要他们保持扩展名,就不在乎有不同的名字)。
这一点,例如,如果我想删除包含字符串的行“这”和“MB”在同一时间的第一列,想要的结果将是:
This is a sentence, Info 1, Info 2, Info 3,...
This is a letter: r, Info 2, Info 3,...
Here theres 'mb' too, Info 1, Info 2, Info 3,...
我知道如何用给定扩展名的单个文件来完成它。例如,对于一个.csv:
import csv
import os
col = 0
look_for1 = set(['This'])
look_for2 = set(['mb'])
# Writing info wanted
with open('./Directory/file.csv','rb') as inf, \
open('./Directory/other_file.csv','wb') as outf:
incsv = csv.reader(inf, delimiter=',')
outcsv = csv.writer(outf, delimiter=',')
outcsv.writerows(row for row in incsv if look_for1 in row[col] and
look_for2 in[col])
os.remove('./Directory/file.csv')
,以及如何列出
import glob
files = glob.glob("*.extensionwanted")
for filename in files
print filename
但在这种情况下,扩展不与该文件夹中通过所有文件.csv和我想要循环延期。 我有点失落在动态环境中执行此操作,而不是使用静态文件名。有人能帮我一把吗?
输出必须转到每个文件inputp的唯一文件或文件输出? – Trimax
“我有点失落”太含糊 - 你想要做什么方面让你烦恼? – martineau
@Informatico_Sano扩展文件没有意义。重要的是它的格式。来自毕尔巴鄂的一个人给了这个文件两个踢腿,并且它已经修复了。 _Ahíva la hostia!_ ;-) – Trimax