结合路径和文件输入模块 [英] combining the path and fileinput modules

查看:46
本文介绍了结合路径和文件输入模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

新手到python编写脚本来递归目录树并删除

文件的第一行(如果它包含给定字符串)。我在运行OS X 10.4.8和FreeBSD 6.1的Mac上得到相同的

错误。


这是脚本:


#程序启动


#p.pl - 修复电子邮件文件中损坏的SMTP标头



#从dir递归并搜索所有子目录

#为每个文件,评估第一行是否以From开头

#为每个匹配,程序删除行


导入文件输入

导入os

导入重新

导入字符串

import sys

来自路径导入路径


#recurse dirs

dir = path(/ home / wsbs / Maildir)
在dir.walkfiles中的
('''''):



#test:

#print f



#打开文件,搜索,必要时更改,写备份

for fileinput.input(f,inplace = 1,backup =''。bak''):

#只检查第一行

if fileinput.isfirstline():

如果不是re.search(''来自'','行):

print line.rstrip(''\ n'')

#只打印所有其他行

如果不是fileinput.isfirstline():

print line.rstrip(''\ n'')

fileinput.close()

#程序结束


脚本产生此错误:


Traceback(大多数最近的呼叫最后一次):

文件" ./ p",第22行,在?/ / $
for fileinput.input(f,inplace = 1,backup =' '.bak''):

文件" /sw/lib/python2.4/fileinput.py" ;,第231行,下一页

line = self.readline ()

文件" /sw/lib/python2.4/fileinput.py",第300行,在readline中

os.rename(self._filename,self。 _backupfilename)

OSError:[Errno 21]是一个目录


如果我取消注释该测试例程,并注释掉fileinput的东西,

程序是否打印变量f的完整路径名/文件名。


非常感谢为什么fileinput.input不喜欢f。

解决方案



wo_shi_big_stomach写道:


新手python编写脚本来递归目录树并删除

文件的第一行如果它包含一个给定的字符串。我在运行OS X 10.4.8和FreeBSD 6.1的Mac上得到相同的

错误。


这是脚本:


#程序启动


#p.pl - 修复电子邮件文件中损坏的SMTP标头



#从dir递归并搜索所有子目录

#为每个文件,评估第一行是否以From开头

#为每个匹配,程序删除行


导入文件输入

导入os

导入重新

导入字符串

import sys

来自路径导入路径


#recurse dirs

dir = path(/ home / wsbs / Maildir)
在dir.walkfiles中的
('''''):



#test:

#print f



你是否绝对确定f list不包含

目录的任何路径,而不是文件?

添加:


f =过滤器(os.path.isfile,f)


并再试一次。




#打开文件,搜索,必要时更改,写备份

for fileinput .input(f,inplace = 1,backup =''。bak''):

#只检查第一行

if fileinput.isfirstline():

如果不是re.search(''来自'','行):

print line.rstrip(''\ n'')

#只打印所有其他行

如果不是fileinput.isfirstline():

print line.rstrip(''\ n'')

fileinput.close()

#程序结束



-

HTH,

Rob


11月23日上午6:15,Rob Wolfe写道:


wo_shi_big_stomach写道:


>新手到python编写脚本来递归目录树并删除
文件的第一行它包含一个给定的字符串。我在运行OS X 10.4.8和FreeBSD 6.1的Mac上遇到同样的错误。

这是脚本:

#start of program

#p.pl - 修复电子邮件文件中损坏的SMTP标头

#dirrses from dir并搜索所有子目录
#查看每个文件,评估第一行是否开始与From
#为每场比赛,程序删除行

导入文件输入
导入os
导入re
导入字符串
从路径导入路径导入系统

#recurse dirs
dir = path(/ home / wsbs / Maildir)
for d indir.walkfiles(''* ''):

#test:
#print f



你绝对确定f列表没有吗包含

目录的任何路径,而不是文件?

添加:


f = filter(os.path.isfile,f )


并再试一次。



对不起,没有快乐。打印f然后产生:


rppp

rppppp

rppppp

rpppr

rppppp

rpppP

rppppp

rppppp


我向你保证不是这个目录中的文件名。


我用f和f.name尝试了这个。前者打印完整的路径名

和filename;后者只打印文件名。但是下面的fileinput.input()调用都没有工作




我和之前的过滤mod一样出错:


文件" ./ p",第23行,in?

for fileinput.input(f,inplace = 1,backup =''。bak'') :


再次感谢您提供的信息fileinput.input()


>


> #
#打开文件,搜索,必要时进行更改,为fileinput.input中的行写入备份(f,inplace = 1,backup =''。bak''):
#check仅限第一行
如果fileinput.isfirstline():
如果不是re.search(''来自'',行):
print line.rstrip(''\ n''' )
#只打印所有其他行
如果不是fileinput.isfirstline():
print line.rstrip(''\ n'')
fileinput.close()<程序结束


在星期四23/11/2006 12:21,wo_shi_big_stomach写道:


dir = path(/ home / wsbs / Maildir)

for d in dir。 walkfiles('''''):



#test:

#print f



您是否绝对确定f list不包含

目录的任何路径,而不是文件?

添加:


f =过滤器(os.path.isfile,f)


并再试一次。


抱歉,没有快乐。打印f然后产生:

rppp
rppppp
rppppp



过滤器应该应用于walkfiles。这样的事情:


dir = path(/ home / wsbs / Maildir)

for f in filter(os.path.isfile,dir.walkfiles( ''*'')):



#test:

#print f

-

Gabriel Genellina

Softlab SRL


__________________________________________________

Correo Yahoo!

Espacio para todos tus mensajes,antivirus y antispam?gratis!

?Abrítucuenta ya! - http://correo.yahoo.com.ar


Newbie to python writing a script to recurse a directory tree and delete
the first line of a file if it contains a given string. I get the same
error on a Mac running OS X 10.4.8 and FreeBSD 6.1.

Here''s the script:

# start of program

# p.pl - fix broken SMTP headers in email files
#
# recurses from dir and searches all subdirs
# for each file, evaluates whether 1st line starts with "From "
# for each match, program deletes line

import fileinput
import os
import re
import string
import sys
from path import path

# recurse dirs
dir = path(/home/wsbs/Maildir)
for f in dir.walkfiles(''*''):
#
# test:
# print f
#
# open file, search, change if necessary, write backup
for line in fileinput.input(f, inplace=1, backup=''.bak''):
# check first line only
if fileinput.isfirstline():
if not re.search(''^From '',line):
print line.rstrip(''\n'')
# just print all other lines
if not fileinput.isfirstline():
print line.rstrip(''\n'')
fileinput.close()
# end of program

The script produces this error:

Traceback (most recent call last):
File "./p", line 22, in ?
for line in fileinput.input(f, inplace=1, backup=''.bak''):
File "/sw/lib/python2.4/fileinput.py", line 231, in next
line = self.readline()
File "/sw/lib/python2.4/fileinput.py", line 300, in readline
os.rename(self._filename, self._backupfilename)
OSError: [Errno 21] Is a directory

If I uncomment that test routine, and comment out the fileinput stuff,
the program DOES print the full pathname/filename for the variable f.

Many thanks for clues as to why fileinput.input doesn''t like f.

解决方案


wo_shi_big_stomach wrote:

Newbie to python writing a script to recurse a directory tree and delete
the first line of a file if it contains a given string. I get the same
error on a Mac running OS X 10.4.8 and FreeBSD 6.1.

Here''s the script:

# start of program

# p.pl - fix broken SMTP headers in email files
#
# recurses from dir and searches all subdirs
# for each file, evaluates whether 1st line starts with "From "
# for each match, program deletes line

import fileinput
import os
import re
import string
import sys
from path import path

# recurse dirs
dir = path(/home/wsbs/Maildir)
for f in dir.walkfiles(''*''):
#
# test:
# print f

Are you absolutely sure that f list doesn''t contain
any path to directory, not file?
Add this:

f = filter(os.path.isfile, f)

and try one more time.

#
# open file, search, change if necessary, write backup
for line in fileinput.input(f, inplace=1, backup=''.bak''):
# check first line only
if fileinput.isfirstline():
if not re.search(''^From '',line):
print line.rstrip(''\n'')
# just print all other lines
if not fileinput.isfirstline():
print line.rstrip(''\n'')
fileinput.close()
# end of program

--
HTH,
Rob


On 11/23/06 6:15 AM, Rob Wolfe wrote:

wo_shi_big_stomach wrote:

>Newbie to python writing a script to recurse a directory tree and delete
the first line of a file if it contains a given string. I get the same
error on a Mac running OS X 10.4.8 and FreeBSD 6.1.

Here''s the script:

# start of program

# p.pl - fix broken SMTP headers in email files
#
# recurses from dir and searches all subdirs
# for each file, evaluates whether 1st line starts with "From "
# for each match, program deletes line

import fileinput
import os
import re
import string
import sys
from path import path

# recurse dirs
dir = path(/home/wsbs/Maildir)
for f in dir.walkfiles(''*''):
#
# test:
# print f


Are you absolutely sure that f list doesn''t contain
any path to directory, not file?
Add this:

f = filter(os.path.isfile, f)

and try one more time.

Sorry, no joy. Printing f then produces:

rppp
rppppp
rppppp
rpppr
rppppp
rpppP
rppppp
rppppp

which I assure you are not the filenames in this directory.

I''ve tried this with f and f.name. The former prints the full pathname
and filename; the latter prints just the filename. But neither works
with the fileinput.input() call below.

I get the same error with the filtered mod as before:

File "./p", line 23, in ?
for line in fileinput.input(f, inplace=1, backup=''.bak''):

Thanks again for info on what to feed fileinput.input()

>

> #
# open file, search, change if necessary, write backup
for line in fileinput.input(f, inplace=1, backup=''.bak''):
# check first line only
if fileinput.isfirstline():
if not re.search(''^From '',line):
print line.rstrip(''\n'')
# just print all other lines
if not fileinput.isfirstline():
print line.rstrip(''\n'')
fileinput.close()
# end of program


At Thursday 23/11/2006 12:21, wo_shi_big_stomach wrote:

dir = path(/home/wsbs/Maildir)
for f in dir.walkfiles(''*''):
#
# test:
# print f

Are you absolutely sure that f list doesn''t contain
any path to directory, not file?
Add this:

f = filter(os.path.isfile, f)

and try one more time.


Sorry, no joy. Printing f then produces:

rppp
rppppp
rppppp

The filter should be applied to walkfiles. Something like this:

dir = path(/home/wsbs/Maildir)
for f in filter(os.path.isfile, dir.walkfiles(''*'')):
#
# test:
# print f
--
Gabriel Genellina
Softlab SRL

__________________________________________________
Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam ?gratis!
?Abrí tu cuenta ya! - http://correo.yahoo.com.ar


这篇关于结合路径和文件输入模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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