Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
testing an open file
#1
what kind of test can i apply (or what library should i look at) to be able to detect if a given open file is open for reading? it is passed as an argument to a function so the function does get to directly see how the file was opened.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
you can get the mode (and much more) by using stats

for example (run from script path -- modify path as required --, probably better to use pathlib):
modified from example here: https://docs.python.org/3/library/os.html#os.scandir
import os

path = os.getcwd()
with os.scandir(path) as it:
    for entry in it:
        if not entry.name.startswith('.') and entry.is_file():
            stats = os.stat(path, follow_symlinks=False)
            print(stats)
see https://docs.python.org/3/library/stat.html on how to interpret the stat results.
Reply
#3
If the file was opened with the builtin function open(), it may have a mode attribute
>>> f = open('temp.tex')
>>> f.mode
'r'
Reply
#4
i was wanting how it was opened, not what permissions the file allows me to open. it might be open to a non-file object like:
Output:
lt1a/forums/3 /home/forums 4> python3.8 Python 3.8.10 (default, Nov 14 2022, 12:59:47) [GCC 9.4.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import sys >>> sys.stdin.mode,sys.stderr.mode,sys.stdout.mode ('r', 'w', 'w') >>> lt1a/forums/3 /home/forums 5>
what cases where it may not have a mode attribute?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#5
(Dec-17-2022, 06:47 PM)Skaperen Wrote: what cases where it may not have a mode attribute?
When the (pseudo) file object was not opened by the function open(), for example
>>> import io
>>> f = io.StringIO()
>>> f.mode
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: '_io.StringIO' object has no attribute 'mode'
Reply
#6
it's an open file object. i want to see if i can read it without trying to read it. i think i might need the same for write.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#7
Objets subclassing io.IOBase have a .readable() method. This also applies to StringIO.

Pseudo file objects provided by third party code may have no readable() or writable() method.
Reply
#8
so, i can just test for mode or a method and if neither exist, just raise an exception saying that readability cannot be determined. that should be workable for most common cases, i think. or am i guessing too far?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Open/save file on Android frohr 0 340 Jan-24-2024, 06:28 PM
Last Post: frohr
  file open "file not found error" shanoger 8 1,165 Dec-14-2023, 08:03 AM
Last Post: shanoger
  How can i combine these two functions so i only open the file once? cubangt 4 877 Aug-14-2023, 05:04 PM
Last Post: snippsat
  I cannot able open a file in python ? ted 5 3,391 Feb-11-2023, 02:38 AM
Last Post: ted
  I get an FileNotFouerror while try to open(file,"rt"). My goal is to replace str decoded 1 1,414 May-06-2022, 01:44 PM
Last Post: Larz60+
  Dynamic File Name to a shared folder with open command in python sjcsvatt 9 6,063 Jan-07-2022, 04:55 PM
Last Post: bowlofred
  Open an excel file Newbie1114 1 2,354 Jun-16-2021, 09:11 PM
Last Post: Gribouillis
  How to open MIDI-file and get events in a list? philipbergwerf 7 5,028 May-29-2021, 08:24 AM
Last Post: j.crater
  Error on open of file created with tempfile.TemporaryDirectory() Brian177 4 6,316 Apr-05-2021, 07:12 PM
Last Post: Brian177
  I can't open a python file :( Oshadha 2 3,011 Mar-28-2021, 11:00 PM
Last Post: pythonprogrammer1101935

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020