利用QFileDialog打开文件对话框
python代码:
1 import sys 2 from PyQt5.Qt import QApplication,QWidget,QFileDialog 3 def getDir(): 4 ''' 5 利用pyqt5 获取目录路径 6 return:str 7 ''' 8 app = QApplication(sys.argv) 9 path = QFileDialog.getExistingDirectory() 10 return path 11 12 if __name__ == '__main__': 13 ret = getDir() 14 print(ret)
应用:
在指定的文件夹下自动创建文件!
1 import sys 2 from PyQt5.Qt import QApplication,QWidget,QFileDialog 3 def getDir(): 4 ''' 5 利用pyqt5 获取目录路径 6 return:str 7 ''' 8 app = QApplication(sys.argv) 9 path = QFileDialog.getExistingDirectory() 10 return path 11 12 if __name__ == '__main__': 13 import os 14 path = getDir() 15 os.chdir(path) 16 for i in range(100): 17 new_fileName = "file_{}.txt".format(i) 18 with open(new_fileName,'w') as f: 19 f.write("My number is {}!\n".format(i)) 20 f.write("Hello World!\n") 21 f.write("Life is short,I learn Python!")