How to open a Folder in Windows Explorer by Python script?
Asked Answered
P

1

5

i'm learning python, and i'm bit stuck...

This is my code:

# TESTING FILE
import os
import subprocess
from pathlib import Path


# VAR
name = 'my_random_dir'


# Main
path2 = str(Path(__file__).parent.absolute())
var = path2 + "/" + name 
print(var)
subprocess.Popen(r'explorer /select "{var}"')

I would like open a directory inside the folder script, that is generated automatically (that would like mean, i can not know the name of this folder previusly and i need to link it from the variables)

I have tried some stuff like the code above, but i didn't found the solution ... is there any way for open a folder in Windows Explorer, when you don't know the name of the folder and you need to take it from variables?

This script only launch Windows Explorer and ignore my path ... is there any syntax error ? Am i approaching it bad?

Predicate answered 17/10, 2020 at 11:37 Comment(2)
you can take it from the user using the following instead of 'name ="..."' . use this name=input("type in your directory")Aloin
@SadafShafi Thank you for the reply! Unfortunately, as i said in the comment below, it doesn't workPredicate
A
7

Use this script

import os
path = "C:\\Users\\shafi\\Desktop\\PAPER"
path = os.path.realpath(path)
os.startfile(path)

and it opens the folder PAPER

Remember to use // instead of /

Aloin answered 17/10, 2020 at 11:48 Comment(7)
Hey, thank you for the reply! Unfortunately, it doesn't work :/Predicate
hey, i've checked the last edit, still doesn't work :/Predicate
@Predicate I am sure you're doing something else wrong, for me its working Do you run it from the comand line and if so, are you in the same folder as that of the python fileAloin
Hey! I've copy paste your code ... still not working ... i've created a video, just for show you what i mean: youtu.be/aC1sm8VnBzEPredicate
thanks for the video buddy. when you write the name of the directory ,write it in quotes. like this >"mydir"Aloin
Hey man! I've re-try, without success :(. I've created another video (youtu.be/G9IR4PGf4d0), i can not really understand where is the error ... i would like learn python, but seems C is more easy than it 😂😂Predicate
@Predicate I am sorry budd, i was focusing on wrong thing See my new edit.Aloin

© 2022 - 2024 — McMap. All rights reserved.