简体   繁体   English

从某个位置加载.txt文件,以便我可以读取内容

[英]Load a .txt file from a location so i can read the content

I am pretty new to programming and I have a task for my university. 我对编程非常陌生,我有一份要上大学的任务。 I want to ask the user to add a .txt file so i can in some way edit(copy the content) and return it edited. 我想请用户添加一个.txt文件,以便我可以某种方式进行编辑(复制内容)并返回已编辑的内容。 I've already tried some solutions but i am stuck. 我已经尝试过一些解决方案,但我遇到了麻烦。

from tkinter import *
from tkinter import filedialog
import os

filename = filedialog.askopenfile()
print(filename.name) 
# I have the location of the loaded file C:/Users/...Desktop/text.txt

nameOfFile = os.path.basename(filename.name)
print(nameOfFile) 
# Here i take the text.txt name 

------

here i want the code to load this text.txt 
file knowing its location so i can have acces to it and read it.

-------

fileReadyToRead = open(nameOfFile, 'r')
file_contents = fileReadyToRead.read()
print(file_contents)

fileReadyToRead.close()

Conclusion : i want to ask user to add a .txt in program and edit the content. 结论:我想请用户在程序中添加.txt并编辑内容。

If you just want the user to select a .txt file and then print its content this works fine : 如果只希望用户选择一个.txt文件,然后打印其内容,则效果很好:

from tkinter import filedialog

filename = filedialog.askopenfile()
fileReadyToRead = open(filename.name, 'r')
file_contents = fileReadyToRead.read()
print(file_contents)
fileReadyToRead.close()

If you want to open a TKinter instance that allows the user to open, edit and save a .txt file this works : 如果您想打开一个允许用户打开,编辑和保存.txt文件的TKinter实例,则可以这样做:

from tkinter import *
from tkinter import filedialog
import codecs


class App():
    def __init__(self, parent):
        self.root = parent
        self.entry = Text(self.root)
        self.entry.pack() 
        self.button1 = Button(self.root, text='Load', command=self.load_txt)
        self.button1.pack()
        self.button2 = Button(self.root, text='Save', command=self.save_txt)
        self.button2.pack()

    def run(self):
        self.root.mainloop() 

    def load_txt(self):   
        self.filename = filedialog.askopenfile()
        with codecs.open(self.filename.name, 'r') as f:
            file_contents = f.read()
            self.entry.insert(INSERT,file_contents) 

    def save_txt(self):
        text = self.entry.get("1.0",END) 
        with codecs.open(self.filename.name, 'w') as f:
            f.write(text)


app = App(Tk())
app.run()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从txt文件提取某些内容? - How can I extract certain content from a txt file? 如何从 python 中的 txt 文件加载代理? - How can i load proxies from a txt file in python? 如何读取以 µ 作为分隔符的 txt 文件? - How can I read a txt file with µ as delimiter? 无法从 txt 文件中读取 python - can't read from txt file python 我如何编写我的 python 代码,以便对 .txt 文件中的数字求和? - How can i write my python code so that it sums the numbers from a .txt file? 为什么我尝试将 txt 文件中的内容读入字典失败? - Why are my attempts to read content from txt file into dictionary failing? 如何使用 Python 从特定位置读取文件到特定位置? - How can I read from a file with Python from a specific location to a specific location? 如何从 a.txt 文件中读取某些字符并将它们写入 Python 中的 a.csv 文件? - How can I read certain characters from a .txt file and write them to a .csv file in Python? 当文件大小改变时,我可以从 txt 文件中读取最后一行吗? (在蟒蛇中) - Can I read the last line from txt file when the file's size change? (in python) 如何使用 python 从 S3 存储桶读取 .txt 文件并查看内容? - How can I read .txt file from S3 bucket using python and view the contents?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM