简体   繁体   中英

How to store the path of a directory from tkinter and then use it later?

I have wrote this code until now but it throws an error saying 'TypeError: stat: path should be string, bytes, os.PathLike or integer, not _io.TextIOWrapper'. I have tried lot of things like tweaking the path from the txt like - "G:/xyz/xyz", "G://xyz//xyz" or r"G:/xyz/xyz", or r"G://xyz//xyz".

import shutil
a = open('file1.txt', 'r')
a1.read()
a.close()

b= open ('file2.txt', 'r')
b1 = b.read()
b.close()

shutil.move(src=a1, dst=b1)

To answer your Question in the title: Use filedialog from tkinter to get a path by the user and store it as a variable.

import tkinter as tk
from tkinter import filedialog as fd

root = tk.Tk()

UserPath = fd.askdirectory()
print(UserPath)

root.mainloop()

to read the lines with pathlib you need to do it like this:

from pathlib import Path

path = Path('path_to_file')
file = path
with file.open() as f:
    print(f.readline())

take a look at this article here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM