1

I want to read a text file using python. I've done it already in my old laptop and desktop computer, but when I try it on new system, error appears: file directory is not valid and file doesn't exist.

txtFile = open("D:/folder/m.txt")

I can't even install external libraries which contains the code above! I am using windows 8 and latest python 2.7

edit: guys, my main problem is the open() method always returns "file directory is not valid","There is no such a file in that directory" even when it's completely valid and exist! I tied it with various files and directories and it doesn't work at all. it works on my old systems but not working on this new laptop. I can't even use external libraries because the open() method is not working in anywhere even in libraries. please help me.

Ali Crash
  • 458
  • 2
  • 5
  • 15
  • yes i have tried that, and also just mention name of file that is in same directory with python script file like this: open("m.txt") – Ali Crash Dec 20 '15 at 14:31

6 Answers6

2

Try this:

txtFile = open("D:\folder\m.txt", 'r')

'r' for reading.

Kenly
  • 24,317
  • 7
  • 44
  • 60
1

If you type on Windows, you should always use \ instead of / when input the path of a file.

Also, always notice the Escape Character. Use \\ if you are not sure.

Err...we can also add an r before the path.

So, try this below.

txtFile = open("D:\\folder\\m.txt")
txtFile = open(r"D:\folder\m.txt")

Both of them should work.

jnjaby
  • 25
  • 1
  • 6
1
with open('file.txt', 'r+') as f:

builtins.py says:

Character Meaning
--------- ---------------------------------------------------------------
'r'       open for reading (default)
'w'       open for writing, truncating the file first
'x'       create a new file and open it for writing
'a'       open for writing, appending to the end of the file if it exists
'b'       binary mode
't'       text mode (default)
'+'       open a disk file for updating (reading and writing)
'U'       universal newline mode (deprecated)
Marcelo Guedes
  • 1,419
  • 11
  • 10
0

Try this:

txtFile = open('D:\\folder\\m.txt', 'r')

Edit#1: you can use os.getcwd() to get the current working directory (import os) and then you won't have to use the slashes at all.

Edit#2: If all else fails, I would refer you to here where I think you can find what you're looking for.

Edit#3: It's a directory?! You never said that. I now officially don't understand what is your goal here.

Community
  • 1
  • 1
Idos
  • 15,053
  • 14
  • 60
  • 75
  • even if this code works, it can't help me. I want to install an external library "tweepy" and the setup file of this library is full of open() methods refers to local files! I can't change all of them! it's an standard form of code and should work. we have to solve real problem. – Ali Crash Dec 20 '15 at 14:36
  • I don't completely understand your issue, but I edited and maybe this is what you need. – Idos Dec 20 '15 at 14:39
  • I edited my question too, the link you mentioned is not exactly what i need. I installed the tweepy library for python tens of times. but in this new laptop which runs windows 8.1, I can't install it because every time curser reaches the open() method error happens and ruins the hole story. – Ali Crash Dec 20 '15 at 14:54
  • @AliCrash I'm sorry, the code I posted should (and will) work if the file exist in that location, I just tried it on my computer and another one... I don't know what could cause this, but this is 100% the way that it would open correctly. – Idos Dec 20 '15 at 16:02
0

Is that the actual name because it look like a directory. If you want to open a file make sure to open it in a mode as well.Make sure to open the file with it's actual name, otherwise I dunno??

  • If you don't have any files with the same name as m.txt why don't you try to open it without the directory, don't know if it will work but just a suggeston –  Dec 20 '15 at 14:53
  • Also most computers start the directory from Users/your name/desktop etc –  Dec 20 '15 at 14:54
  • I tried to open in every forms with directory without directory any form you can imagine. the main problem is the open() is not working at all! – Ali Crash Dec 20 '15 at 14:56
  • Oh I just remembered why don't you move the python file you are working on and put it in the same folder as the one you want to open maybe that will work unless you have already tried it of course, then you won't need the directory as well –  Dec 20 '15 at 14:59
  • The code you have given doesn't show you have opened it in read or write mode you need to do that –  Dec 20 '15 at 15:01
0

An old question I know.
Can be defined in the system record "Open with ".
But the extension may be undefined, in such cases just use the file name and never write the extension.
For more information, investigate MIME-TYPES and Default-Aplication.

dsgdfg
  • 1,492
  • 11
  • 18