Ask coding questions

← Back to all posts
How do I ask the user to open a file from their computer?
MuffinsTheCat (18)

I'm making a program that requires the user to upload a file from their computer. I know I can use something like Documents/text.txt to open a specific file but I want the user to choose which file they open from their computer. How do I do this in Python?

Comments
hotnewtop
TalinSharma (67)

What language?

If it is HTML Then you could do something like this:


The HTML

<form action="the_script_below.php" method="post">
<input type="file" value="Upload A File" name="file" required>
<br><br>
<input type="submit">
</form>

The PHP

<?php
$file = $_POST['file'];
#Do Something With The File Such As Making A Copy
?>

Hope this helps and if it doesn't please specify a language!

RYANTADIPARTHI (6006)

solution

I don't know about their file, but may this:

userinput = input('file : ')
f = open(userinput, 'r'):
  print(f.read())

like that.

MuffinsTheCat (18)

@RYANTADIPARTHI
I tried this but it gives me an invalid syntax error

RYANTADIPARTHI (6006)

@MuffinsTheCat hmm, then i don't think it is possible.