内容
aabbcc字符串1fdasfe
ccddee字符串2FDSAFE
要找第一行中字符串1,用来替换掉第二行中字符串2后存盘
8 个解决方案
#1
没明白。
#2
def fun(path):
for root,dirs,files in os.walk(path):
for fn in files:
rootpath=root+fn
print (rootpath)
f=open(rootpath,'r')
f.close()
fun(r'E:\workspace\python')
为啥open里用rootpath不行啊?
for root,dirs,files in os.walk(path):
for fn in files:
rootpath=root+fn
print (rootpath)
f=open(rootpath,'r')
f.close()
fun(r'E:\workspace\python')
为啥open里用rootpath不行啊?
#3
import os
def fun(path):
for root,dirs,files in os.walk(path):
for fn in files:
rootpath = os.path.join(root,fn)
print(rootpath)
f=open(rootpath,'r')
f.close()
fun(r'E:\workspace\python')
#4
额,楼上的可以
取一个目录下所有log文件,并将指定行替换成该文件中某处的一个字符串.怎么找到那个字符串,见下面ProxyHostbbb=,这个字符串怎么提取出来啊?
import os,re
def fun(path):
for root,dirs,files in os.walk(path):
for fn in files:
rootpath = os.path.join(root,fn)
filetype=fn.split('.')[1]
if filetype in['log']:
f= open('fg.ini','r+')
d=f.read()
open('fg.ini', 'w').write(re.sub(r'ProxyHostaaaa=', 'ProxyHostbbb=', d))
f.close()
fun(r'E:\workspace\python')
取一个目录下所有log文件,并将指定行替换成该文件中某处的一个字符串.怎么找到那个字符串,见下面ProxyHostbbb=,这个字符串怎么提取出来啊?
import os,re
def fun(path):
for root,dirs,files in os.walk(path):
for fn in files:
rootpath = os.path.join(root,fn)
filetype=fn.split('.')[1]
if filetype in['log']:
f= open('fg.ini','r+')
d=f.read()
open('fg.ini', 'w').write(re.sub(r'ProxyHostaaaa=', 'ProxyHostbbb=', d))
f.close()
fun(r'E:\workspace\python')
#5
import glob, os
def getFiles(path, filefile):
''' 返回指定目录、指定文件扩展名的文件列表 '''
return glob.glob(os.path.join(path, '*%s'%fileext))
files = getFiles(r'../log', '.log')
#6
zhu@ubuntu:~/python$ python file.py
ProxyHostaaaa=192.168.1.1
ProxyHostbbb192.168.1.1
zhu@ubuntu:~/python$ cat file.py
zhu@ubuntu:~/python$
ProxyHostaaaa=192.168.1.1
ProxyHostbbb192.168.1.1
zhu@ubuntu:~/python$ cat file.py
#!/usr/bin/env python
import re
d = 'ProxyHostaaaa=192.168.1.1'
t = re.sub(r'(ProxyHostaaaa)=', 'ProxyHostbbb', d)
print d
print t
zhu@ubuntu:~/python$
#7
zhu@ubuntu:~/python$
zhu@ubuntu:~/python$ python file.py
ProxyHostaaaa=192.168.1.1
ProxyHostbbb=192.168.1.1
zhu@ubuntu:~/python$ cat file.py
zhu@ubuntu:~/python$
zhu@ubuntu:~/python$ python file.py
ProxyHostaaaa=192.168.1.1
ProxyHostbbb=192.168.1.1
zhu@ubuntu:~/python$ cat file.py
#!/usr/bin/env python
import re
d = 'ProxyHostaaaa=192.168.1.1'
t = re.sub(r'ProxyHostaaaa=', 'ProxyHostbbb=', d)
print d
print t
zhu@ubuntu:~/python$
#8
非常感谢各位的热心,写一个小小的程序问题一大堆
这不,又有问题了,下面程序读写英文名文件可以运行,怎么中文就不行了啊?
# -*- coding=utf-8 -*-
import os,re,linecache
def fun(path):
for root,dirs,files in os.walk(path):
for fn in files:
rootpath=os.path.join(root,fn)
filetype =fn.split('.')[1]
if filetype in['testcase']:
print('filepath'+rootpath)
new=(linecache.getline(rootpath,13)).rstrip()
print('newtext'+new)
bt='XmlScripts.+createCrossConnections'
a=re.search(bt,new)
ss=(a.group())
print(ss)
newsp=ss.split('.')
print('newtestx:'+newsp[2])
f=open(rootpath,'r+')
d=f.read()
open(rootpath,'w').write(re.sub('testCaseCharacter','<testCaseCharacter>25_NBI/NBI_北向接口/XML/PTN'+newsp[2]+'.'+newsp[2]+'</testCaseCharacter>',d))
f.close()
else:
continue
fun(r'D:\TEST')
Python 3.2.2 (default, Sep 4 2011, 09:51:08) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
filepathD:\TEST\1_创建Static类型的静态Tunnel_节点类型Ingress_环境预置.testcase
newtext <mLogicClass>XmlScripts.HighLineCases.ConnectionControl.createCrossConnections</mLogicClass>
XmlScripts.HighLineCases.ConnectionControl.createCrossConnections
newtestx:ConnectionControl
Traceback (most recent call last):
File "D:\test2.py", line 24, in <module>
fun(r'D:\TEST')
File "D:\test2.py", line 19, in fun
d=f.read()
UnicodeDecodeError: 'gbk' codec can't decode bytes in position 154-155: illegal multibyte sequence
>>>
这不,又有问题了,下面程序读写英文名文件可以运行,怎么中文就不行了啊?
# -*- coding=utf-8 -*-
import os,re,linecache
def fun(path):
for root,dirs,files in os.walk(path):
for fn in files:
rootpath=os.path.join(root,fn)
filetype =fn.split('.')[1]
if filetype in['testcase']:
print('filepath'+rootpath)
new=(linecache.getline(rootpath,13)).rstrip()
print('newtext'+new)
bt='XmlScripts.+createCrossConnections'
a=re.search(bt,new)
ss=(a.group())
print(ss)
newsp=ss.split('.')
print('newtestx:'+newsp[2])
f=open(rootpath,'r+')
d=f.read()
open(rootpath,'w').write(re.sub('testCaseCharacter','<testCaseCharacter>25_NBI/NBI_北向接口/XML/PTN'+newsp[2]+'.'+newsp[2]+'</testCaseCharacter>',d))
f.close()
else:
continue
fun(r'D:\TEST')
Python 3.2.2 (default, Sep 4 2011, 09:51:08) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
filepathD:\TEST\1_创建Static类型的静态Tunnel_节点类型Ingress_环境预置.testcase
newtext <mLogicClass>XmlScripts.HighLineCases.ConnectionControl.createCrossConnections</mLogicClass>
XmlScripts.HighLineCases.ConnectionControl.createCrossConnections
newtestx:ConnectionControl
Traceback (most recent call last):
File "D:\test2.py", line 24, in <module>
fun(r'D:\TEST')
File "D:\test2.py", line 19, in fun
d=f.read()
UnicodeDecodeError: 'gbk' codec can't decode bytes in position 154-155: illegal multibyte sequence
>>>
#1
没明白。
#2
def fun(path):
for root,dirs,files in os.walk(path):
for fn in files:
rootpath=root+fn
print (rootpath)
f=open(rootpath,'r')
f.close()
fun(r'E:\workspace\python')
为啥open里用rootpath不行啊?
for root,dirs,files in os.walk(path):
for fn in files:
rootpath=root+fn
print (rootpath)
f=open(rootpath,'r')
f.close()
fun(r'E:\workspace\python')
为啥open里用rootpath不行啊?
#3
import os
def fun(path):
for root,dirs,files in os.walk(path):
for fn in files:
rootpath = os.path.join(root,fn)
print(rootpath)
f=open(rootpath,'r')
f.close()
fun(r'E:\workspace\python')
#4
额,楼上的可以
取一个目录下所有log文件,并将指定行替换成该文件中某处的一个字符串.怎么找到那个字符串,见下面ProxyHostbbb=,这个字符串怎么提取出来啊?
import os,re
def fun(path):
for root,dirs,files in os.walk(path):
for fn in files:
rootpath = os.path.join(root,fn)
filetype=fn.split('.')[1]
if filetype in['log']:
f= open('fg.ini','r+')
d=f.read()
open('fg.ini', 'w').write(re.sub(r'ProxyHostaaaa=', 'ProxyHostbbb=', d))
f.close()
fun(r'E:\workspace\python')
取一个目录下所有log文件,并将指定行替换成该文件中某处的一个字符串.怎么找到那个字符串,见下面ProxyHostbbb=,这个字符串怎么提取出来啊?
import os,re
def fun(path):
for root,dirs,files in os.walk(path):
for fn in files:
rootpath = os.path.join(root,fn)
filetype=fn.split('.')[1]
if filetype in['log']:
f= open('fg.ini','r+')
d=f.read()
open('fg.ini', 'w').write(re.sub(r'ProxyHostaaaa=', 'ProxyHostbbb=', d))
f.close()
fun(r'E:\workspace\python')
#5
import glob, os
def getFiles(path, filefile):
''' 返回指定目录、指定文件扩展名的文件列表 '''
return glob.glob(os.path.join(path, '*%s'%fileext))
files = getFiles(r'../log', '.log')
#6
zhu@ubuntu:~/python$ python file.py
ProxyHostaaaa=192.168.1.1
ProxyHostbbb192.168.1.1
zhu@ubuntu:~/python$ cat file.py
zhu@ubuntu:~/python$
ProxyHostaaaa=192.168.1.1
ProxyHostbbb192.168.1.1
zhu@ubuntu:~/python$ cat file.py
#!/usr/bin/env python
import re
d = 'ProxyHostaaaa=192.168.1.1'
t = re.sub(r'(ProxyHostaaaa)=', 'ProxyHostbbb', d)
print d
print t
zhu@ubuntu:~/python$
#7
zhu@ubuntu:~/python$
zhu@ubuntu:~/python$ python file.py
ProxyHostaaaa=192.168.1.1
ProxyHostbbb=192.168.1.1
zhu@ubuntu:~/python$ cat file.py
zhu@ubuntu:~/python$
zhu@ubuntu:~/python$ python file.py
ProxyHostaaaa=192.168.1.1
ProxyHostbbb=192.168.1.1
zhu@ubuntu:~/python$ cat file.py
#!/usr/bin/env python
import re
d = 'ProxyHostaaaa=192.168.1.1'
t = re.sub(r'ProxyHostaaaa=', 'ProxyHostbbb=', d)
print d
print t
zhu@ubuntu:~/python$
#8
非常感谢各位的热心,写一个小小的程序问题一大堆
这不,又有问题了,下面程序读写英文名文件可以运行,怎么中文就不行了啊?
# -*- coding=utf-8 -*-
import os,re,linecache
def fun(path):
for root,dirs,files in os.walk(path):
for fn in files:
rootpath=os.path.join(root,fn)
filetype =fn.split('.')[1]
if filetype in['testcase']:
print('filepath'+rootpath)
new=(linecache.getline(rootpath,13)).rstrip()
print('newtext'+new)
bt='XmlScripts.+createCrossConnections'
a=re.search(bt,new)
ss=(a.group())
print(ss)
newsp=ss.split('.')
print('newtestx:'+newsp[2])
f=open(rootpath,'r+')
d=f.read()
open(rootpath,'w').write(re.sub('testCaseCharacter','<testCaseCharacter>25_NBI/NBI_北向接口/XML/PTN'+newsp[2]+'.'+newsp[2]+'</testCaseCharacter>',d))
f.close()
else:
continue
fun(r'D:\TEST')
Python 3.2.2 (default, Sep 4 2011, 09:51:08) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
filepathD:\TEST\1_创建Static类型的静态Tunnel_节点类型Ingress_环境预置.testcase
newtext <mLogicClass>XmlScripts.HighLineCases.ConnectionControl.createCrossConnections</mLogicClass>
XmlScripts.HighLineCases.ConnectionControl.createCrossConnections
newtestx:ConnectionControl
Traceback (most recent call last):
File "D:\test2.py", line 24, in <module>
fun(r'D:\TEST')
File "D:\test2.py", line 19, in fun
d=f.read()
UnicodeDecodeError: 'gbk' codec can't decode bytes in position 154-155: illegal multibyte sequence
>>>
这不,又有问题了,下面程序读写英文名文件可以运行,怎么中文就不行了啊?
# -*- coding=utf-8 -*-
import os,re,linecache
def fun(path):
for root,dirs,files in os.walk(path):
for fn in files:
rootpath=os.path.join(root,fn)
filetype =fn.split('.')[1]
if filetype in['testcase']:
print('filepath'+rootpath)
new=(linecache.getline(rootpath,13)).rstrip()
print('newtext'+new)
bt='XmlScripts.+createCrossConnections'
a=re.search(bt,new)
ss=(a.group())
print(ss)
newsp=ss.split('.')
print('newtestx:'+newsp[2])
f=open(rootpath,'r+')
d=f.read()
open(rootpath,'w').write(re.sub('testCaseCharacter','<testCaseCharacter>25_NBI/NBI_北向接口/XML/PTN'+newsp[2]+'.'+newsp[2]+'</testCaseCharacter>',d))
f.close()
else:
continue
fun(r'D:\TEST')
Python 3.2.2 (default, Sep 4 2011, 09:51:08) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
filepathD:\TEST\1_创建Static类型的静态Tunnel_节点类型Ingress_环境预置.testcase
newtext <mLogicClass>XmlScripts.HighLineCases.ConnectionControl.createCrossConnections</mLogicClass>
XmlScripts.HighLineCases.ConnectionControl.createCrossConnections
newtestx:ConnectionControl
Traceback (most recent call last):
File "D:\test2.py", line 24, in <module>
fun(r'D:\TEST')
File "D:\test2.py", line 19, in fun
d=f.read()
UnicodeDecodeError: 'gbk' codec can't decode bytes in position 154-155: illegal multibyte sequence
>>>