假设有一个英文文本文件,编写一个程序读取其内容并将里面的大写字母变成小写字母,小写字母变成大写字母

def transform(filename):
    file = filename + ".txt"
    f = open(file,"r+")
    fn = open("{}转化后.txt".format(filename),"w")
    for line in f:
        for word in line:
            if word in "abcdefghijklmnopqrstuvwxyz":
                word = word.upper()
            elif word in "ABCDEFGHIJKLMNOPQRSTUVWXYZ":
                word = word.lower()
            fn.write(word)
    f.close()
    fn.close()

def main():
    filename = input("请输入文件名称:")
    transform(filename)

main()

 

 

你可能感兴趣的:(python)