Python如何从父目录上的文件夹中读取TXT文件

发布于 2025-01-31 12:32:48 字数 633 浏览 5 评论 0原文

我是Python的新手,试图同时学习和编码,以测试我可以做的事情,我在课程上学习了Java,JavaScript,PHP,HTML,CSS,因此我仍然回复了基础知识。

我达到了这个问题,几个小时后我找不到我能理解和喜欢的解决方案。

这是我的结构: 我的结构

我想在test_input.py中阅读test_input.txt用户有一些字符串,我希望这些字符串根据语言更改。我虽然要在.py文件的侧面写入.txt,但是每次函数都会生成字符串,我需要再次制作所有语言文件夹,也需要添加其他语言,我还会在每个字符串上制作各种文件夹发生。

如果可能的话,我想要一个可以读取自己内部的项目以获取.txt文件的解决方案,因为我希望此项目成为.EXE桌面程序。另外,Pyhton可以制作简单的桌面应用程序吗?我看起来像我在Java中学到的Android一样学习未来的语言,但是我想使用Kotlyn,因为“更好”,所以我以博学的习惯在Java中做到了这个项目,并且在过去做了一些事情,但是我想要“将来最常使用的内容”。

如果我错了,请在任何事情上纠正我,所有这些都更多地是关于我能做什么,以及如何感谢您的帮助!!!

I'm new to python, trying to learn and code at the same time, to test what i can do, I learned java, javascript, php, html, css, on my course, so I still remeber the basics.

I reached this problem and after hours i haven't found a solution that I can understand and like.

So this is my structure:
my structure

I want to read the test_input.txt inside the test_input.py, i want that because there are some strings for the user, and i want those strings to change based on the language. I though to write the .txt along side the .py file, but then everytime a function would generate string I would need to make all the language folders again, also if needed to add another language, I also would make various folders on every string occurency.

If possible, i want a solution that read the project inside itself to get the .txt file, because i want this project to be an .exe desktop program. Also, is pyhton good to make simple desktop apps? I'm lookin foward to learn the future languages, like I learned android in java, but I want to use kotlyn because is "better", so I cold make this project in java as a learned and did some in the past, but I want the "what will be most used on the future".

Please correct me in anything if I'm wrong, all this is more about see what I can do, and how, thanks for the help!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烈酒灼喉 2025-02-07 12:32:48

如果我正确理解您,您想加载并读取PY中的TXT文件。如我所知,如果是这种情况,那么也许您想在此处遵循本教程:
https://www.pypythontutorial.net/python-basics.net/python-basics/python-basics/python -Read-text-file/

另外,您是否已经尝试打开/加载它?如果是这样,您会遇到错误吗?在大多数情况下,对于初学者来说,这是一个路径问题,因此请确保已经设置了路径。

干杯

If I understand you correctly, you want to load and read the txt file in py. If this is the case, like i understood, then perhaps you want to follow this tutorial here:
https://www.pythontutorial.net/python-basics/python-read-text-file/

Also, did you try to open/load it already? If so, did you get an error? Most of the time, it is a path problem for beginners so make sure the path is setup already.

Cheers

恬淡成诗 2025-02-07 12:32:48

我从geeksforgeeks那里得到了这个脚本,它有多种形式的读取.txt,我也将您的文档留给您。

# Program to show various ways to read and
# write data in a file.
file1 = open("myfile.txt","w")
L = ["This is Delhi \n","This is Paris \n","This is London \n"] 
  
# \n is placed to indicate EOL (End of Line)
file1.write("Hello \n")
file1.writelines(L)
file1.close() #to change file access modes
  
file1 = open("myfile.txt","r+") 
  
print("Output of Read function is ")
print(file1.read())
print()
  
# seek(n) takes the file handle to the nth
# bite from the beginning.
file1.seek(0) 
  
print( "Output of Readline function is ")
print(file1.readline()) 
print()
  
file1.seek(0)
  
# To show difference between read and readline
print("Output of Read(9) function is ") 
print(file1.read(9))
print()
  
file1.seek(0)
  
print("Output of Readline(9) function is ") 
print(file1.readline(9))
  
file1.seek(0)
# readlines function
print("Output of Readlines function is ") 
print(file1.readlines()) 
print()
file1.close()

https://www.geeksforgeeks.org/reading-writing-writing-writing-writing-writing-text-files -python/

I got this script from geeksforgeeks, it got multiple form of how to read a .txt, I am leaving you as well the documentation.

# Program to show various ways to read and
# write data in a file.
file1 = open("myfile.txt","w")
L = ["This is Delhi \n","This is Paris \n","This is London \n"] 
  
# \n is placed to indicate EOL (End of Line)
file1.write("Hello \n")
file1.writelines(L)
file1.close() #to change file access modes
  
file1 = open("myfile.txt","r+") 
  
print("Output of Read function is ")
print(file1.read())
print()
  
# seek(n) takes the file handle to the nth
# bite from the beginning.
file1.seek(0) 
  
print( "Output of Readline function is ")
print(file1.readline()) 
print()
  
file1.seek(0)
  
# To show difference between read and readline
print("Output of Read(9) function is ") 
print(file1.read(9))
print()
  
file1.seek(0)
  
print("Output of Readline(9) function is ") 
print(file1.readline(9))
  
file1.seek(0)
# readlines function
print("Output of Readlines function is ") 
print(file1.readlines()) 
print()
file1.close()

https://www.geeksforgeeks.org/reading-writing-text-files-python/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文