gpt4 book ai didi

python - 将第一个文件转换为另一个文件

转载 作者:行者123 更新时间:2023-11-28 17:13:00 25 4
gpt4 key购买 nike

我是编程新手,在将第一个文件转换为另一个文件方面需要帮助。任务是:

Write a program that asks the user for two filenames. The first one should mark any existing text file. The second filename may be new, so the file with this name may not exist.

The program's task is to take the first file of the file, convert it to capital letters, and write another file.

到目前为止我有:

file_old  = input("Which file do you want to take ? ")
file_new = input("In which file do you want to put the content? ")

file1 = open(file_old, encoding="UTF-8")
file2 = open(file_new, "w")

for rida in file1:
file2.write(rida.upper())

file1.close()
file2.close()

The error pic

最佳答案

您必须写入文件的完整路径才能使您的代码正常工作。

我对其进行了测试,它运行良好。

输入路径应该是这样的

C:\Users\yourUserName\PycharmProjects\test_folder\test_small_letters.txt

这应该代替您输入的old.txt

例如:

"C:\Program Files\Python36\python.exe" C:/Users/userName/PycharmProjects/pythonSnakegame/test_file_capitalize.py
which file you want to take ? C:\Users\userName\PycharmProjects\test_folder\test_small_letters.txt
In which file you want to put the content? C:\Users\userName\PycharmProjects\test_folder\test_big_letters.txt
C:\Users\userName\PycharmProjects\test_folder\test_small_letters.txt
C:\Users\userName\PycharmProjects\test_folder\test_big_letters.txt

Process finished with exit code 0

新文件已创建并大写。

关于python - 将第一个文件转换为另一个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46183329/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com