-1

Using the Python os Module, it is not opening this File (bouncpass.txt). When I run the code, nothing appears, I don't get no error or nothing. What is wrong with the below code?

import os

bouncfile = os.open('C:\\hoopers\\pickup\\towns\\bouncpass.txt', os.O_RDONLY)
martineau
  • 119,623
  • 25
  • 170
  • 301
Calculate
  • 329
  • 1
  • 5
  • 19
  • 2
    That code will print nothing if it is correct and no error. Can you do print(bouncfile) and see? – mathtick Feb 25 '22 at 17:55
  • Or better yet run it in the REPL (IPython) – mathtick Feb 25 '22 at 17:55
  • 2
    What do you mean, "nothing appears"? You opened a file, but then what? Are you trying to read from it? – chepner Feb 25 '22 at 17:55
  • 2
    `os.open` does not open the file in some default text editor, if that's what you are expecting. – chepner Feb 25 '22 at 17:56
  • 2
    If you don't know what to do with `bouncfile`, then you probably want to use `open`, not `os.open`. – chepner Feb 25 '22 at 17:57
  • 1
    you can use simple open() method to openfile. This code has no error. – M. Twarog Feb 25 '22 at 18:00
  • Hello , thankyou for the responses. I was thinking that when opening the file, it would actually open up the file automatically on my desktop. Are you guys saying that in order to do this, you would have to "read" the file as well ? – Calculate Feb 26 '22 at 18:10
  • mathtick, when i print(bouncfile), it produces the number 3. – Calculate Feb 26 '22 at 18:22

2 Answers2

1
import os
bouncfile = os.open('C:\\hoopers\\pickup\\towns\\bouncpass.txt', os.O_RDONLY)
str = os.read(bouncfile, os.path.getsize(bouncfile))
print(str.decode())

with os.open() file you need to use os.read() to read the content of file. I use dummy file so check ouptut screen enter image description here

bouncfile = open('some.txt', 'r')
for val in bouncfile.readlines():
    print(val.rstrip())

you can use simple open() method to open file and you don't need read method to read content file with simple open() method.

Output: enter image description here

M. Twarog
  • 2,418
  • 3
  • 21
  • 39
  • 1
    Please don't post screenshots of code or text output, include them as text. Also, note that you should always use the `open` function except if you have a good reason to use `os.open` (see https://stackoverflow.com/questions/17051249/os-open-vs-open-what-to-use). If you want to read a whole file, use the `read` method of the file object. Note also that `for line in f.readlines():` is an anti-pattern in Python, as it uselessly loads the whole file before iterating on the lines, while `for line in f:` will also iterate on the lines, but lazily. – Thierry Lathuille Feb 25 '22 at 20:03
  • 1
    ah, i see, this is another way of scrapping. Very good to know. thanks. As i mentioned in new comment i made above i was thinking that by using os.open, it could actually open the file on my computer automatically. – Calculate Feb 26 '22 at 18:18
  • if you find the solution to your problem you can mark it as answer. Thanks and we are here to explain you everything where you got stuck while learning – M. Twarog Feb 26 '22 at 20:24
0

There may be a reason you are choosing os over open().

But as M. Twarog mentioned, you can bypass the need of the os library to open and read files by using the open() built-in method in python.

Just for additional info, another way of using open() is by using the with keyword in Python.

with open('\path\to\text', 'r') as f:
    info = f.read()

What is the python keyword "with" used for?

FlippinBits
  • 155
  • 7