+ -
当前位置:首页 → 问答吧 → 如何将一个文件中的字母全转换成大写?

如何将一个文件中的字母全转换成大写?

时间:2011-07-23

来源:互联网

大家好, 我是新手, 想请教一个问题
如何将一个文件中的字母全转换成大写?
下面这是我写的, 出了问题.正确的该怎么写? 不知道更简洁的写法是怎么样的?

with open('filename', mode='r+') as file:
content = file.read()
content = content.upper()
file.truncate(0)
file.write(content)


作者: firebugdotname   发布时间: 2011-07-23

Python code

with open('1.txt', mode='r+') as file:
    content = file.read()
    content = content.upper()
    #print content
    file.truncate(0)
    file.seek(0)  //要加这个,把文件指针放到文件开头
    file.write(content)
    file.close()


作者: hanyuwei0   发布时间: 2011-07-23