如果没有进行替换,Python字符串会替换文件而不触及文件 [英] Python string replace in a file without touching the file if no substitution was made

查看:170
本文介绍了如果没有进行替换,Python字符串会替换文件而不触及文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果没有字符串替换,Python的string.replace会返回什么结果?
即使没有更改,Python的file.open(f,'w')总是会触及文件吗?

使用Python,我试图用一组文件中的newtext替换oldtext的出现。如果一个文件包含oldtext,我想做替换并保存文件。否则,什么也不做,所以文件保持其旧的时间戳。

下面的代码工作正常,除了所有文件被写入,即使没有字符串替换,所有文件有一个新的时间戳。

 用于匹配all_files('*。html','。'):#all_files return all当前目录中的html文件
thefile = open(match)
content = thefile.read()#将整个文件读入内存
thefile.close()
thefile = open ,'w')
thefile.write(content.replace(oldtext,newtext))#用
写文件thefile.close()

在这段代码中,我试图只在发生字符串替换时才执行file.write,但所有文件仍然得到一个新的时间戳:

  count = 0 
用于all_files('*。html','。')中的匹配:#all_files returns all当前目录中的html文件
thefile = open (match)
content = thefile.read()#将整个文件读入内存
thefile.close()
thefile = open(match,'w')
replaText = content .replace(oldtext,newtext)
if replacedText!='':
count + = 1
thefile.write(replacedText)
thefile.close()
print (count)#打印我们修改的文件数

最后count是总数的文件数量,而不是修改的文件数量。有什么建议么?谢谢。



我在Windows上使用Python 3.1.2。


如果没有字符串替换是
,Python的string.replace
会返回什么?


$ b


Python的file.open(f,'w')总是
触摸文件,即使
没有变化?

不仅仅是触摸文件,它破坏任何内容<$因此,你可以用来测试文件是否需要重写,如果replatedText!= content ,并且只在这种情况下以写模式打开文件:

  count = 0 
用于all_files('*。html','。')中的匹配:#all_files返回当前目录中的所有html文件
打开(匹配)作为文件:
content = thefile.read()#读取整个fi le到内存中
replacementText = content.replace(oldtext,newtext)
if replaceText!= content:
with open(match,'w')as thefile:
count + = 1
thefile.write(replacedText)
print(count)#打印我们修改的文件数


What does Python's string.replace return if no string substitution was made? Does Python's file.open(f, 'w') always touch the file even if no changes were made?

Using Python, I'm trying to replace occurrences of 'oldtext' with 'newtext' in a set of files. If a file contains 'oldtext', I want to do the replacement and save the file. Otherwise, do nothing, so the file maintains its old timestamp.

The following code works fine, except all files get written, even if no string substitution was made, and all files have a new timestamp.

for match in all_files('*.html', '.'):  # all_files returns all html files in current directory     
  thefile = open(match)
  content = thefile.read()              # read entire file into memory
  thefile.close()
  thefile = open(match, 'w')             
  thefile.write(content.replace(oldtext, newtext))  # write the file with the text substitution
  thefile.close()

In this code I'm trying to do the file.write only if a string substitution occurred, but still, all the files get a new timestamp:

count = 0
for match in all_files('*.html', '.'):       # all_files returns all html files in current directory
    thefile = open(match)
    content = thefile.read()                 # read entire file into memory
    thefile.close()
    thefile = open(match, 'w')
    replacedText = content.replace(oldtext, newtext) 
    if replacedText != '':
        count += 1
        thefile.write(replacedText)
    thefile.close()
print (count)        # print the number of files that we modified

At the end, count is the total number of files, not the number of files modified. Any suggestions? Thanks.

I'm using Python 3.1.2 on Windows.

解决方案

What does Python's string.replace return if no string substitution was made?

It returns the original string.

Does Python's file.open(f, 'w') always touch the file even if no changes were made?

More than merely touching the file, it destroys any content f used to contain.

So, you can test if the file needs to be rewritten with if replacedText != content, and only open the file in write mode if this is the case:

count = 0
for match in all_files('*.html', '.'):       # all_files returns all html files in current directory
    with open(match) as thefile:
        content = thefile.read()                 # read entire file into memory
        replacedText = content.replace(oldtext, newtext)
    if replacedText!=content:
        with open(match, 'w') as thefile:
            count += 1
            thefile.write(replacedText)
print (count)        # print the number of files that we modified

这篇关于如果没有进行替换,Python字符串会替换文件而不触及文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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