原文链接[脚本之家](http://www.jb51.net/article/64669.htm)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
import
os
def
replace(filePath, w2u):
try
:
oldfile
=
open
(filePath,
"rb+"
)
#这里必须用b打开
path, name
=
os.path.split(filePath)
newfile
=
open
(path
+
'$'</code> <code class="py keyword">+</code> <code class="py plain">name, </code><code class="py string">"ba+"</code><code class="py plain">)</code></div><div class="line number8 index7 alt1"><code class="py spaces"> </code> </div><div class="line number9 index8 alt2"><code class="py spaces"> </code><code class="py plain">old </code><code class="py keyword">=</code> <code class="py plain">b''</code></div><div class="line number10 index9 alt1"><code class="py spaces"> </code><code class="py plain">new </code><code class="py keyword">=</code> <code class="py plain">b''</code></div><div class="line number11 index10 alt2"><code class="py spaces"> </code><code class="py keyword">if</code> <code class="py plain">w2u </code><code class="py keyword">=</code><code class="py keyword">=</code> <code class="py color1">True</code><code class="py plain">:</code></div><div class="line number12 index11 alt1"><code class="py spaces"> </code><code class="py plain">old </code><code class="py keyword">=</code> <code class="py plain">b</code><code class="py string">'\r'</code></div><div class="line number13 index12 alt2"><code class="py spaces"> </code><code class="py plain">new </code><code class="py keyword">=</code> <code class="py plain">b''</code></div><div class="line number14 index13 alt1"><code class="py spaces"> </code><code class="py keyword">else</code><code class="py plain">:</code></div><div class="line number15 index14 alt2"><code class="py spaces"> </code><code class="py plain">old </code><code class="py keyword">=</code> <code class="py plain">b</code><code class="py string">'\n'</code></div><div class="line number16 index15 alt1"><code class="py spaces"> </code><code class="py plain">new </code><code class="py keyword">=</code> <code class="py plain">b</code><code class="py string">'\r\n'</code></div><div class="line number17 index16 alt2"> </div><div class="line number18 index17 alt1"><code class="py spaces"> </code><code class="py plain">data </code><code class="py keyword">=</code> <code class="py plain">b''</code></div><div class="line number19 index18 alt2"><code class="py spaces"> </code><code class="py keyword">while</code> <code class="py plain">(</code><code class="py color1">True</code><code class="py plain">):</code></div><div class="line number20 index19 alt1"><code class="py spaces"> </code><code class="py plain">data </code><code class="py keyword">=</code> <code class="py plain">oldfile.read(</code><code class="py value">200</code><code class="py plain">)</code></div><div class="line number21 index20 alt2"><code class="py spaces"> </code><code class="py plain">newData </code><code class="py keyword">=</code> <code class="py plain">data.replace(old, new)</code></div><div class="line number22 index21 alt1"><code class="py spaces"> </code><code class="py plain">newfile.write(newData)</code></div><div class="line number23 index22 alt2"><code class="py spaces"> </code><code class="py keyword">if</code> <code class="py functions">len</code><code class="py plain">(data) < </code><code class="py value">200</code><code class="py plain">:</code></div><div class="line number24 index23 alt1"><code class="py spaces"> </code><code class="py keyword">break</code></div><div class="line number25 index24 alt2"><code class="py spaces"> </code><code class="py plain">newfile.close()</code></div><div class="line number26 index25 alt1"><code class="py spaces"> </code><code class="py plain">oldfile.close()</code></div><div class="line number27 index26 alt2"><code class="py spaces"> </code> </div><div class="line number28 index27 alt1"><code class="py spaces"> </code><code class="py plain">os.remove(filePath)</code></div><div class="line number29 index28 alt2"><code class="py spaces"> </code><code class="py plain">os.rename(path </code><code class="py keyword">+</code> <code class="py string">'$'
+
name, filePath)
except
IOError as e:
print
(e)
if
__name__
=
=
"__main__"
:
print
(
"请输入文件路径:"
)
filePath
=
input
()
replace(filePath,
False
)
#这个改为True就可以实现\n变成\r\n
|
要注意的是,在python里,像\r\n这样的符号,如果是文本打开的话,是找不到\r\n的,而只能找到’\n’,所以必须用b(二进制)模式打开。
<div class="art_xg">
您可能感兴趣的文章:
- wxPython 入门教程
- wxPython中文教程入门实例
- wxpython 最小化到托盘与欢迎图片的实现方法
- windows下wxPython开发环境安装与配置方法
- 使用wxpython实现的一个简单图片浏览器实例
- 将图片文件嵌入到wxpython代码中的实现方法
- python之wxPython菜单使用详解
- wxPython事件驱动实例详解
- wxPython窗口中文乱码解决方法
- wxPython定时器wx.Timer简单应用实例
- python去除文件中空格、Tab及回车的方法
- wxpython中Textctrl回车事件无效的解决方法