question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

getting file path

See original GitHub issue

Hello, i have an input of type file, i want to get the path from the input to my python script, for example electron provides an additional input.files[0].path field that allows for this to happen, since the default web browser path would look like this c:/fakepath/filename.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6

github_iconTop GitHub Comments

7reactions
ylmazmehmet60commented, Dec 5, 2018

For security reasons it is not possible to get the file path through the browser as @ChrisKnott said. Using the Tkinter library, you can easily do it as shown in the example below.

app.py

@eel.expose
def btn_ResimyoluClick():
	root = Tk()
	root.withdraw()
	root.wm_attributes('-topmost', 1)
	folder = filedialog.askdirectory()
	return folder

main.html

<script>
async function getFolder() {
var dosya_path = await eel.btn_ResimyoluClick()();
	if (dosya_path) {
		console.log(dosya_path);
	}
}
</script>
<a onclick="getFolder()">GetFolder</a>
1reaction
gburletcommented, Aug 15, 2020

I ran into this issue as well. Was hoping for some sort of solution like Electron as the OP mentioned. However, I understand why it doesn’t exist. I wasn’t a fan of the solutions given because you have to modify the UI and having a web frontend UI is the whole reason for the Eel library.

From the JS side we have the filename, last modified date, and file size in bytes. You can search filesystem on Python side for name match and weed out duplicates based on modified date & file size. Alternatively, you can calculate a checksum hash from the base64 file data on JS side and use that for searching on Python side. I’ll implement this and post here for those interested. I can’t imagine the run time is going to be great.

Edit: I implemented this two ways:

  1. When the file is selected on HTML side, call readAsArrayBuffer and send the byte array as a variable to python via eel exposed function that is then saved to a temporary file and therefore you know the path. Downsides: redundant data. Takes a LONG time for the data to be passed to Python even for files < 1MB.
  2. The solution I thought of above, doing a file search on Python side. It’s also way too slow, even on my SSD and using heuristics for search locations.

I’ll try setting electron as the client-side browser and maybe I can get access to the file path field it attaches as @ChrisKnott suggested and report back.

Edit 2: following #57 and the Electron-Eel minimal example I was able to get Electron up and running as my client with Eel. All <input type="file"> now have a path variable attached to them with the absolute path. A little bit of setup but works great and no hacky solutions necessary. This is the way to go 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Copy the Full Path of a File on Windows 10
Find the file or folder whose path you'd like to copy in File Explorer. Hold down Shift on your keyboard and right-click on...
Read more >
How to get the complete path to a file or folder on Windows 10
Paths are needed when you have to move a Command Prompt or PowerShell window to a different directory or when you need to...
Read more >
Show the Full Folder Path in File Explorer on Windows 10
Show the Full Folder Path in File Explorer on Windows 10 · 2. Click Options · 3. Select Change folder and search options,...
Read more >
How to get full path of a file? - linux - Stack Overflow
Holding Shift and right clicking on a file in Windows Explorer gives you an option called Copy as Path . This will copy...
Read more >
Get(FilePath) - FileMaker Pro
In Windows, the full path is file:/drive:/folder/filename for local files. For remote files, the full path is file://volume/folder/filename.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found