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!

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....

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 !

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.

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.