Is there a way to taker user input as a txt file and then rewrite it into a new txt file?

huangapple go评论52阅读模式
英文:

Is there a way to taker user input as a txt file and then rewrite it into a new txt file?

问题

这是不工作的代码:

outputString = " "

userinput = input('Enter the name of the file you want to read:' + " ")

myfile = open(userinput, 'r')

myOutputFile = open(userinput_Output, 'a')


for line in myfile:
    words = line.split()

    for current in words:
        myOutputFile.write(current + "\n")
        
myfile.close()

myOutputFile.close()

这是可以工作的代码:

outputString = " "

myfile = open("readme.txt", 'r')

myOutputFile = open("readme_Output.txt", 'a')


for line in myfile:
    words = line.split()

    for current in words:
        myOutputFile.write(current + "\n")
        
myfile.close()

myOutputFile.close()

希望这可以帮助您解决问题。

英文:

I am writing a program where the user inputs their txt file. The txt file is read and then the txt file is written into a new txt file. The new txt file has one word on each line. I am able to write the program without the user input and it works perfectly. Once I add the user input it tells me the txt file does not exist in my directory (even though it does). Am I not sure what I am doing wrong. Please point me in the direction of easy tutorials I can watch to solve this problem.

This is the code that doesn't work

outputString = " "

userinput = input('Enter the name of the file you want to read:' + " ")

myfile = open(userinput, 'r')

myOutputFile = open(userinput_Output, 'a')


for line in myfile:
    words = line.split()

    for current in words:
        myOutputFile.write(current + "\n")
        
myfile.close()

myOutputFile.close()

This is the code that works (you can test it out with your own txt files and see that it does actually work if you don't want to take my word for it.)

outputString = " "

myfile = open("readme.txt", 'r')

myOutputFile = open("readme_Output.txt", 'a')


for line in myfile:
    words = line.split()

    for current in words:
        myOutputFile.write(current + "\n")
        
myfile.close()

myOutputFile.close()

答案1

得分: 0

The reason why is because userinput_Output is not defined.

What you need to do is concatenate userinput to _Output.txt.

I have flagged where the correction would go in the code below.

userinput = input('输入要读取的文件名: ')

# 打开文件
路径 = 'C:/path/to/folder/with/files/'
我的文件 = open(路径 + userinput+'.txt', 'r')
我的输出文件 = open(路径 + userinput+"_output.txt", 'a')   # <---  这行

forin 我的文件:
    单词 =.split()

    for 当前 in 单词:
        我的输出文件.write(当前 + "\n")

# 关闭文件
我的文件.close()
我的输出文件.close()

这里是数据结果。

示例输入文本文件 (test.txt):

你好 世界
你好 月亮

生成的输出文本文件 (test_output.txt):

你好
世界
你好
月亮

作为额外信息,你可以使用上下文管理器同时打开这两个文件(自动处理关闭),如下所示,更加清晰:

userinput = input('输入要读取的文件名: ')

路径 = 'C:/path/to/folder/with/files/'
输入文件 = os.path.join(路径, userinput + '.txt')
输出文件 = os.path.join(路径, userinput + '_output.txt')

with open(输入文件, 'r') as myfile, open(输出文件, 'a') as myOutputFile:
    forin myfile:
        单词 =.split()
        for 当前 in 单词:
            myOutputFile.write(当前 + "\n")
英文:

The reason why is because userinput_Output is not defined.

What you need to do is concatenate userinput to _Output.txt.

I have flagged where the correction would go in the code below.



userinput = input(&#39;Enter the name of the file you want to read: &#39;)

# open the files
path = &#39;C:/path/to/folder/with/files/&#39;
myfile = open(path + userinput+&#39;.txt&#39;, &#39;r&#39;)
myOutputFile = open(path + userinput+&quot;_output.txt&quot;, &#39;a&#39;)   # &lt;---  this line


for line in myfile:
    words = line.split()

    for current in words:
        myOutputFile.write(current + &quot;\n&quot;)
        
# close the files
myfile.close()
myOutputFile.close()

Here are the data results.

example input text file (test.txt):

hello world
hello moon

resulting output text file (test_output.txt):

hello
world
hello
moon

As an extra, you can open both files in a context manager (which handles closing both automatically, like this) which is much cleaner:

userinput = input(&#39;Enter the name of the file you want to read: &#39;)

path = &#39;C:/path/to/folder/with/files/&#39;
input_file = os.path.join(path, userinput + &#39;.txt&#39;)
output_file = os.path.join(path, userinput + &#39;_output.txt&#39;)

with open(input_file, &#39;r&#39;) as myfile, open(output_file, &#39;a&#39;) as myOutputFile:
    for line in myfile:
        words = line.split()
        for current in words:
            myOutputFile.write(current + &quot;\n&quot;)

huangapple
  • 本文由 发表于 2023年7月5日 01:01:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76614644.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定