I've got a Python program able to read files locally just fine:
In the directory where I have this program, there's a file called path_list (it's a list of file paths), and I can open and access it like so:
test_explicit = open('path_list').read()
print 'Reading local file gives: ' + test_explicit
Then the program is going to loop through those paths and call the following function on each path, doing things based on what it finds in the version directory above. Unfortunately, here when I have absolute paths instead of relative, those same open/read operations are giving 'No such file or directory' errors. (But when I print out where it's trying to go and ls there, I see the contents I expect).
Here's the relevant part of my code:
def getCommand(path):
# Grab that trailing /version, strip the v, convert to int
split_path = path.split("/")
version = split_path.pop()
version_num = int (version[1:] )
# Increment that number, and remake path with a fresh /v(x+1) suffix
version_num += 1
new_suffix = '/v' + str(version_num)
higher_file_path = '/'.join(split_path)
higher_file_path += new_suffix
finished_filename = 'finished.txt'
finished_filepath = os.path.join(higher_file_path, finished_filename)
result = open(finished_filepath).read()
print 'Result is: ' + result
[more code]
When I run it I get a failure on the line with open
and read()
:
IOError: [Errno 2] No such file or directory: '~/scripts/test/ABC/v4/finished.txt'
But when I ls
or cd
there I do see the file.