Error when using os.stat - Python
<p><strong>Solved: Adding an os.chdir(myArg) resolved the issue.</strong></p>

<p>I'm getting an error when trying to run the following code on anything other than my home directory or files/direcs that I own.</p>

<p>FileNotFoundError: [Errno 2] No such file or directory: </p>

<p>I created a file in root and changed ownership on the file to pi:pi (user running the script). If I specify that file directly, it works, however if I run the script on "/", it will not read that or any other file/direc.  I also created a directory /tempdir_delete/ and changed ownership to pi:pi.. If I run the script specifically on "/tempdir_delete/*", it works, but if I leave off the * it fails.</p>

<p>Why does it fail on all except /home/pi/ or files that I explicitly specify and own?  It's running the stat as user pi, which is granted by sudo to perform the stat. Also, why do I have to specify the file that I own explicitly? Shouldn't it see that file in root and work because I own it? </p>

<pre><code>import os
import re
import sys
import pwd

myReg = re.compile(r'^\.')
myUID = os.getuid()
myArg = sys.argv[1]

print(os.getuid())
print(pwd.getpwuid(int(myUID)))
print(myArg)

def getsize(direct):
        if os.path.isfile(direct) == True:
                statinfo = os.stat(myArg)
                print(str(statinfo.st_size))
        else:
                for i in os.listdir(direct):
                        try:
                                statinfo = os.stat(i)
                                if myReg.search(i):
                                        continue
                                else:
                                        print(i + ' Size: ' + str(statinfo.st_size))
                        except:
                                print('Exception occurred, can't read.')
                                continue

getsize(myArg)
</code></pre>