英文:
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') # <--- 这行
for 行 in 我的文件:
单词 = 行.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:
for 行 in 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('Enter the name of the file you want to read: ')
# open the files
path = 'C:/path/to/folder/with/files/'
myfile = open(path + userinput+'.txt', 'r')
myOutputFile = open(path + userinput+"_output.txt", 'a') # <--- this line
for line in myfile:
words = line.split()
for current in words:
myOutputFile.write(current + "\n")
# 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('Enter the name of the file you want to read: ')
path = 'C:/path/to/folder/with/files/'
input_file = os.path.join(path, userinput + '.txt')
output_file = os.path.join(path, userinput + '_output.txt')
with open(input_file, 'r') as myfile, open(output_file, 'a') as myOutputFile:
for line in myfile:
words = line.split()
for current in words:
myOutputFile.write(current + "\n")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论