Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
chkFile with absolute paths
Hey everyone,

Here is a function I made to check if files exist.
def chkFile(filesPath):
	if hasattr(filesPath, "__len__"):
		for i in filesPath:
			if not path.exists(i):
				return False
		return True
		return path.exists(filesPath)
it works great for files in the main directories, and subdirectories. However I am trying to get it to recognize a file located in /sys/bus/w1/devices/, which is nowhere near my working dir. anyone know how I can change this to accept absolute dir's?

What kind of object are you passing into the function?

os.path.exists() should have no problems with files outside your current directory.

>>> import os
>>> os.path.exists("/var/tmp")
>>> os.path.exists("/etc/fstab")
Quote:What kind of object are you passing into the function?

a string with the directory I mentioned above

could the os be blocking it? does linux make some Dir's off limits to Python?
I don't understand how that function would work even locally for files of more than 1 character.

If you pass in a string like "local", then it loops over each of the characters. It will check if the path "l" exists, and return False if it doesn't.

What's the purpose of checking if it has a __len__ attribute and then looping over the elements if it does?
You can also add in a list of paths, and it will return False if one is. It good, or True if they are all valid.

Compares the runs the __len__ attribute into the hasattr function to see if its a list or a string
But that's not a valid distinction. Both strings and lists have __len__.

>>> hasattr(list, "__len__")
>>> hasattr(str, "__len__")
So a list will work, but a string won't.

from os import path

def chkFile(filesPath):
    if hasattr(filesPath, "__len__"):
        for i in filesPath:
            if not path.exists(i):
                return False
        return True
        return path.exists(filesPath)

print(chkFile(['/etc/fstab', '/tmp']))
True False
do you have a better suggestion?

I tried it when I made it and it worked, but I will check again to make sure it wasn't a fluke. Python is new to me, and the other languages I have used didn't differentiate between list, truples, and dictionaries.
Most functions just assume that particular type will be sent in (it's always a list-like object or it's always a string).

You can ask the object if it's an instance of a string and otherwise assume you could iterate through it.

def show_strings(obj):
    if isinstance(obj, str):
        print(f"got the single string {obj}")
        for s in obj:
            print(f"One string in the collection is {s}")

show_strings(("string1", "string2", "string3"))
got the single string onestring One string in the collection is string1 One string in the collection is string2 One string in the collection is string3
I don't see any reason that it should work on local files and fail on files in /sys. Does it work for something like "/etc/passwd"? I don't have the file that you're concerned about on my system, so I don't know if it's special in some way.

Possibly Related Threads…
Thread Author Replies Views Last Post
  Absolute beginner am I missing something? kamren 7 472 Sep-25-2020, 05:32 AM
Last Post: buran
  absolute imports between folders mikisDW 0 287 Aug-05-2020, 12:26 PM
Last Post: mikisDW
  Paths millpond 12 975 Jul-30-2020, 01:16 PM
Last Post: snippsat
  Problems with windows paths delphinis 6 599 Jul-21-2020, 06:11 PM
Last Post: Gribouillis
  I am an absolute newbie.Help me! feynarun 4 557 Apr-11-2020, 09:25 PM
Last Post: feynarun
  Shortest paths to win snake and ladder sandaab 5 1,112 Jun-30-2019, 03:20 PM
Last Post: sandaab
  Absolute newbie asking for help with blinking LEDs misterfrickel 0 649 Mar-15-2019, 08:54 AM
Last Post: misterfrickel
  How to handle paths with spaces in the name? zBernie 1 1,004 Nov-22-2018, 04:04 AM
Last Post: ichabod801
  Absolute Beginner Python Question matchamochi7 5 1,481 Nov-02-2018, 12:09 PM
Last Post: wookie
  reading directory paths from file to list malonn 6 1,527 Oct-26-2018, 11:19 PM
Last Post: malonn

Forum Jump:

User Panel Messages

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