读取字符串并将它们输出到控制台不同(Python)

时间:2015-02-21 11:07:41

标签: python python-3.x file-handling

很抱歉标题不知道怎么说这个。

我想要做的是制作一个只能用我的程序编写和读取的代码我已经编写了所以我可以将代码编写到文件中,但我不确定如何在不改变的情况下读取代码文本文件我只希望我的代码打印到控制台或空闲而不更改文本文件,我不是要求你为我写出代码,而是如何去做这个?

print ("Welcome Who am i speaking too?")

User = input("Name: ")

print ("Welcome " +User)

Running = True

def FileReader():
    FileName = input("Please Enter File Name: ")
    file = open (FileName + ".txt" , "r")
    #How Do i go about this?
    print (file.read())
    file.close()


def FileOutput():
    NameOfFile = input("Enter File Name: ")
    File = open(NameOfFile + ".txt", "wt")
    Content = input("Type Your Message: ")
    Content = Content.replace(" ", "SP")
    Content = Content.replace("a", "ASDAFAS")
    Content = Content.replace("b", "ASDAKSGDHLHASJD")
    Content = Content.replace("c", "ASHJDGASGDKJASG")
    Content = Content.replace("d", "JHASGDHASLASHGD")
    Content = Content.replace("e", "AKJ:SBDKJASBDASJDBSDBKL")
    Content = Content.replace("f", "KBASLLASIBDJBASLDJ")
    Content = Content.replace("g", "ASJHDVBLKAJSDBLASJDB")
    Content = Content.replace("h", "JHAKSDHKDKSAJDHASJKSHDA")
    Content = Content.replace("i", "KJABSDJKABSDKJBASKJDBASKJDB")
    Content = Content.replace("j", ":AJSDJASDJKAHSDJHSD")
    Content = Content.replace("k", "OAHISDIUHASIDASDHD")
    Content = Content.replace("l", "UIAHSDUIHSADIUSHADUIYSA")
    Content = Content.replace("m", "ASHDUASHDUHASUDHUIASDHIUHA")
    Content = Content.replace("n", "IUAHSDIUHASIDUIAUSHDHUIS")
    Content = Content.replace("o", "AHJSVDJHSVABDJHVSD")
    Content = Content.replace("p", "IASDIJASIDJAISDJOIAJSD")
    Content = Content.replace("q", "LIUAGSDIUGWVDASID")
    Content = Content.replace("r", "JAHSDJWIUCBASB")
    Content = Content.replace("s", "OIAHSDBIWUBDUIB")
    Content = Content.replace("t", "LAJHSBDHBWQIEUBQWIUW")
    Content = Content.replace("u", "INQWOIDNWQIOND")
    Content = Content.replace("v", "OIHQWEIUCBWOIQBEUWQBEWOQIEB")
    Content = Content.replace("W", "OASIHDOIASDOSAHDAISDH")
    Content = Content.replace("x", "OIAJDIWQDBUIOEHWQE")
    Content = Content.replace("y", "POASJDPOQWEOJWQOEJWQEOP")
    Content = Content.replace("z", "ASJDBSOBDOWBQDWIOD")
    File.write(Content)


while Running:
    What = input("What Would you like to do: ")
    if What == "read":
        FileReader()

    if What == "write":
        FileOutput()

    if What == "close":
        Running = False

1 个答案:

答案 0 :(得分:0)

Himal是对的。

file.read()
file.close()