pyqt QFileDialog.getOpenFileName() get path of the directory of the file

时间:2016-04-15 11:06:40

标签: python directory pyqt qfiledialog getopenfilename

I'm searching for a way to get the path of the directory of the file that I have chosen by QFileDialog.getOpenFileName(). I know that you can access it by os.path.dirname(os.path.realpath(filename), but I'm searching for a better way because I need to work in this directory. I don't really understand why you can access the file by open(filename, 'r') though your current working directory (when typing print(os.getcwd()) is not the directory of the file. Maybe there is a way by accessing something like the current working directory of the Qt.Application, but I had no success..

Also I have functions where you need arg1 = directory and arg2 = filename1 (in the directory) as arguments. Funnily enough they suddenly seem to work with just(!) arg1 = 'C:' as directory and arg2 = filename2 when filename2 is the file I've accessed by QFileDialog.getOpenFileName().

I'm happy about any explanation!

1 个答案:

答案 0 :(得分:1)

事实证明,QFileDialog.getOpenFileName()访问的文件名实际上不仅是文件名而是整个路径..