0

Hi everybody!

I have a small prob here... I'm trying to assign a directory (folder) path to a variable while evaluating python script. Well, to be more clearer, I need to do run something like:

getDirPath.py 'mypath'

and after that I need to get this path as variable inside my getDirPath.py file, so I can list all the files there for example and so on.

Let's say, I want user to drag-n-drop folder to the getDirPath.py file and he will get files, which are listed from that folder

thank!

3
Contributors
5
Replies
6
Views
8 Years
Discussion Span
Last Post by reyev
0

You can use the raw_input built-in function in your script:

#My script
import os
#blablabla
#...
user_path = raw_input('> ')
list_of_contents = os.listdir(user_path)#the list of all files and folders in the entered path

#Do something with the files in the list obtained here...

to get a path entered by the user at the keyboard during the execution.

Then, inside your script use the user_path string combined I imagine with some os module path manipulation functions.

The drag-and-drop is quite special, I've never heard of people trying to drag-and-drop over running scripts, but maybe if it had an interface....

0

thanks! I have just realized that It's impossible to drag folder onto .py scripts )))
so I guess I should find some other way to do that
maybe I'll try to combine python script with the shell commands, via .bat executable file

thanks anyway !

0

You can do this using sys.argv - use
getDirPath.py 'mypath'
and inside the program
print sys.argv, type(sys.argv)

As for drag and drop, Python is geared more towards a GUI, so you want FileDialog in Tkinter or QFileDialog in qt and the wx tool kit probably has something similiar, to select the file and pass the name to the program.

0

thanks!! sys.argv was exactly what I needed!
just one more question - do you know how can I run a 'python myscript.py' command from a windows .bat file? I'm trying to drag'n'drop my folder to a bat file, and inside this executable I have a command like :

python myscript.py %1

but when I'm trying to execute this I've got an error that myscript.py file is not found

0

sorry I have solved this problem already! this was mine mistake, a simple syntax error
thanks for your help!!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.