Unable to access files using os functions

81 Views Asked by At

I've been working to create a script that compares CSV files (based on this post: https://medium.com/@satsundev/compare-two-csv-using-python-92a0a23d7f54) but have run into an issue where Python can't locate the files I'm comparing. Below is the relevant portion of code I've been working on debugging.

import pandas as pd
import os
import json
import sys

def check_file_permissions(file_path):
    if os.access(file_path, os.R_OK):
        print(f"Read permission is granted for file: {file_path}")
   
    else:
        print(f"Read permission is not granted for file: {file_path}")
    
    if os.access(file_path, os.W_OK):
        print(f"Write permission is granted for file: {file_path}")
   
    else:
        print(f"Write permission is not granted for file: {file_path}")
    
    if os.access(file_path, os.X_OK):
        print(f"Execute permission is granted for file: {file_path}")
   
    else:
        print(f"Execute permission is not granted for file: {file_path}")

cwd = os.getcwd()

cwwd = os.path.join(cwd, "Projects", "compare-csv")

input_JSON = os.path.join(cwwd, "data", "input.json")

input_data = json.load(open(input_JSON))

data = input_data ["info"]


for info in data:
    expected = os.path.join(cwwd, "inputs", "expected", info["expected"])
    actual = os.path.join(cwwd, "inputs", "actual", info["actual"])
    print (actual)
    
    # diff path
    diff = os.path.join(cwwd, "inputs", "diff", info["diff"])
    
    # summary path
    summary = os.path.join(cwwd, "inputs", "diff", info["summary"])
    
    # common identity column with unique value between two CSV files to compare
    identity = info["identity"]
    
    # os.chmod(actual, 777)
    
    check_file_permissions(actual)
    
    actual_path = os.path.exists(actual)
    print(actual_path)

For context, Print(actual) returns the correct path but print(actual_path) returns false.

If relevant, I am using JupyterLab and the Projects folder is located on my desktop. The JSON file is as follows:

{
    "info": [{
        "description": "Placeholder.",
        "expected": "user_info_expected.csv",
        "actual": "user_info_actual.csv",
        "diff": "user_info_diff.csv",
        "summary": "user_info_summary.csv",
        "identity": "id"
    }]
}

I looked into some of the documentation for os functions and found that os.path.exists() typically returns False if permission is not granted to execute os.stat() on the requested file. This is where I wrote check_file_permissions and when I ran it, it returns the negative for all three permissions.

From here, I started looking into os.chmod() which doesn't seem to do anything regardless of what level I try to grant permissions. Looking into my permissions, I have all the required permissions so I'm kind of at a loss.

0

There are 0 best solutions below