0

i have the following problem during file open: Using PyQt QFileDialog I get path for files from user which I would like to read it

def read_file(self):
        self.t_file = (QFileDialog.getOpenFileNames(self, 'Select File', '','*.txt'))

Unfortunately I cannot open a file if the path has numbers in it: Ex:

'E:\test\02_info\test.txt'

I tried

f1 = open(self.t_file,'r')

Could anyone help me to read files from such a path format? Thank you in advance.

EDIT: I get the following error:

Traceback (most recent call last):
  File "<pyshell#27>", line 1, in <module>
    f1 = open(self.t_file,'r')
IOError: [Errno 22] invalid mode ('r') or filename: 'E:\test\x02_info\test.txt'
ekhumoro
  • 115,249
  • 20
  • 229
  • 336
sat0408
  • 73
  • 12
  • Run this to verify if it is a problem with the path. I believe it is not `os.path.exists(os.path.abspath(r'E:\test\02_info\test.txt'))` – Hossain Muctadir Mar 19 '16 at 20:34
  • What version of Windows? of Python? What error message? I have directories named '27' and '35' and have had no problems on Win 7 and Win 10 with multiple python versions. The problem must be something else. – Terry Jan Reedy Mar 19 '16 at 20:40
  • I have edited my post with the error. I am using Windows 7 and python 2.7. Just to verify the problem, i tried it if i have the path as a raw string and i can open the file. – sat0408 Mar 19 '16 at 20:57
  • @sat0408 Try using forward slashes... It looks like your path is interpreted as containing a hex byte `\x02` – jDo Mar 19 '16 at 21:03
  • The problem is that I get the file path as user input which is done through a File Browser. – sat0408 Mar 19 '16 at 21:05
  • @sat0408. In your `read_file()` method, add the line `print(repr(self.t_file))` and show us the output. – ekhumoro Mar 19 '16 at 21:41
  • This is what we'd expect to see if this was a literal string because the `"\02"` is seen as a hex escape for `0x02`. You can get around that with raw strings or double escapes like `"\\02"`. But how did that happen here? `QFileDialog.getOpenFileNames` returns a list, not a single file name, so there is some processing going on here that we don't see. Did you do something exotic like a `ast.literal_eval`? – tdelaney Mar 19 '16 at 21:49
  • @ekhumoro. This is what is printed `` – sat0408 Mar 19 '16 at 21:52
  • Is there any code that modifies `t_file` before the open? BTW, the `"\t"` in the filenames are tabs, so you have more problems than just the numbers. – tdelaney Mar 19 '16 at 21:57
  • http://bugs.python.org/issue3624 You need right system(os level) and python encoding ! Other way : Try `os.walk` check variations on filenames. – dsgdfg Mar 19 '16 at 22:26

1 Answers1

1

The problem is caused by your use of getOpenFileNames (which returns a list of files) instead of getOpenFileName (which returns a single file). You also seem to have converted the return value wrongly, but since you haven't shown the relevant code, I will just show you how it should be done (assuming you are using python2):

def read_file(self):
    filename = QFileDialog.getOpenFileName(self, 'Select File', '','*.txt')
    # convert to a python string
    self.t_file = unicode(filename)
ekhumoro
  • 115,249
  • 20
  • 229
  • 336