递归文件编辑 [英] recursive file editing

查看:99
本文介绍了递归文件编辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个蟒蛇新手;这里有一些与我试图解决的问题相关的问题。如果python是最好的

乐器,或者如果awk或混合使用bash和sed会更好,我会徘徊:


1)我怎么会得到递归下降

通过所有子目录中的所有文件

我的脚本被调用的那个?


2)每个审查的文件应该编辑;如果找到这种类型的字符串


foo.asp = dev?bar(其中bar可以是数字或空格)


它应该总是用这个字符串代替


foo-bar.html(如果bar是一个空格,新字符串是foo-.html)


3)读取的文件名称本身可以是foo.asp = dev?bar;

编辑后的输出文件也应该按照相同的规则重命名

如上所述......或者这可以通过bash脚本更好地处理?


任何提示赞赏


-

Michele Alzetta

解决方案

这是一个Perl单行:


perl -p -i -e's / foo / bar / gi''`find。/`


问候,

- -

Leif Biberg Kristensen
http://solumslekt.org/

Validare necesse est


>我是一个蟒蛇新手;这里有一些与

问题有关的问题我试图解决;如果python是最好的工具,或者如果awk或bash和sed的组合会更好,我会徘徊:

1)我将如何递归下降
通过
我的脚本调用的所有子目录中的所有文件?


查看os.path.walk。


2)每个被检查的文件都应该被编辑;如果找到这种类型的字符串

foo.asp = dev?bar(其中bar可以是数字或空格)

它应该总是用这个代替字符串

foo-bar.html(如果bar为空格,则新字符串为foo-.html)


查看re模块。 />

3)读取的文件名称本身可以是foo.asp = dev?bar;
编辑后的输出文件也应该根据相同的规则重命名。如上所述......或者这会更好地通过bash脚本来处理吗?




不管你感觉更舒服,Python都可以做到。

- Josiah


Il Sat,2004年4月3日22:35:30 +0200,Leif B. Kristensen ha scritto:

这是一个Perl单行:

perl -p -i -e'/ foo / bar / gi''`find ./`




没用;然而我意识到我可以反复运行各种变化


sed -is / foo / bar / g *


然后重命名文件一个名为renna的bash脚本。


我肯定python可以做到但我被卡住了 -


for os.walk中的文件(''mydir''):

打印文件[2]


给我所有文件的名称


但我如何用r +模式打开每个?


for os.walk(''mydir''):

file(thing, mode = r +)


语法无效


-

Michele Alzetta


I''m a python newbie; here are a few questions relative to a
problem I''m trying to solve; I''m wandering if python is the best
instrument or if awk or a mix of bash and sed would be better:

1) how would I get recursively descend
through all files in all subdirectories of
the one in which my script is called ?

2) each file examined should be edited; IF a string of this type is found

foo.asp=dev?bar (where bar can be a digit or an empty space)

it should always be substituted with this string

foo-bar.html (if bar is an empty space the new string is foo-.html)

3) the names of files read may themselves be of the sort foo.asp=dev?bar;
the edited output file should also be renamed according to the same rule
as above ... or would this be better handled by a bash script ?

Any hints appreciated

--
Michele Alzetta

解决方案

This is a Perl one-liner:

perl -p -i -e ''s/foo/bar/gi'' `find ./`

regards,
--
Leif Biberg Kristensen
http://solumslekt.org/
Validare necesse est


> I''m a python newbie; here are a few questions relative to a

problem I''m trying to solve; I''m wandering if python is the best
instrument or if awk or a mix of bash and sed would be better:

1) how would I get recursively descend
through all files in all subdirectories of
the one in which my script is called ?
Check out os.path.walk.

2) each file examined should be edited; IF a string of this type is found

foo.asp=dev?bar (where bar can be a digit or an empty space)

it should always be substituted with this string

foo-bar.html (if bar is an empty space the new string is foo-.html)
Check out the re module.

3) the names of files read may themselves be of the sort foo.asp=dev?bar;
the edited output file should also be renamed according to the same rule
as above ... or would this be better handled by a bash script ?



Do it however you feel more comfortable, Python can do it.
- Josiah


Il Sat, 03 Apr 2004 22:35:30 +0200, Leif B. Kristensen ha scritto:

This is a Perl one-liner:

perl -p -i -e ''s/foo/bar/gi'' `find ./`



Didn''t work; however I realized I could just repeatedly run variations of

sed -i s/foo/bar/g *

and then rename the files with a bash script called renna.

I''m sure python could do it but I got stuck -

for file in os.walk(''mydir''):
print file[2]

gives me the names of all files

but how do I open each with r+ mode ?

for thing in os.walk(''mydir''):
file(thing,mode=r+)

is invalid syntax

--
Michele Alzetta


这篇关于递归文件编辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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