Select to view content in your preferred language

Create Path with OS dirname

459
3
Jump to solution
a week ago
kapalczynski
Frequent Contributor

I can hardcode this with no issues

CSV_FILE = r"C:\\Users\\cccc\\Desktop\\~GIS_projects\\aaaStagingProcessingScripts_dev_tst\\AGOL_User_Expire_Email\\AGOL_Users_ToExpire\\UsersAboutToExpire.csv"

df_new = pd.read_csv(CSV_FILE)

 

I want to grab the Location OA path with this

osPath = os.path.dirname(os.path.abspath(__file__))

CSV_FILE = osPath + r"\\AGOL_Users_ToExpire\\UsersAboutToExpire.csv"

df_new = pd.read_csv(CSV_FILE)

 

BUT the layer does not work... any thoughts?  Seems like a syntax issue but cant figure it out

0 Kudos
1 Solution

Accepted Solutions
CodyPatterson
MVP Regular Contributor

Hey @kapalczynski 

You may want to place some debugging messages in the code to ensure that it's going to exactly what you want, try this out:

osPath = os.path.dirname(os.path.abspath(__file__))
CSV_FILE = os.path.join(osPath, "AGOL_Users_ToExpire", "UsersAboutToExpire.csv")
print(CSV_FILE)  # Debug using this print on the csv file
df_new = pd.read_csv(CSV_FILE)

There may also be an issue with your concatenation, try this out as well:

CSV_FILE = os.path.join(osPath, "AGOL_Users_ToExpire", "UsersAboutToExpire.csv")

Along those lines, could you send the error that's appearing when you try to run this?

Cody

View solution in original post

0 Kudos
3 Replies
CodyPatterson
MVP Regular Contributor

Hey @kapalczynski 

You may want to place some debugging messages in the code to ensure that it's going to exactly what you want, try this out:

osPath = os.path.dirname(os.path.abspath(__file__))
CSV_FILE = os.path.join(osPath, "AGOL_Users_ToExpire", "UsersAboutToExpire.csv")
print(CSV_FILE)  # Debug using this print on the csv file
df_new = pd.read_csv(CSV_FILE)

There may also be an issue with your concatenation, try this out as well:

CSV_FILE = os.path.join(osPath, "AGOL_Users_ToExpire", "UsersAboutToExpire.csv")

Along those lines, could you send the error that's appearing when you try to run this?

Cody

0 Kudos
ArniMiller
New Contributor

1//

0 Kudos
TylerT
by
Frequent Contributor

In addition to @CodyPatterson 's recommendation you could move on to Pythons object oriented path library called Pathlib.  I have found Pathlib more readable and robust.  Your code would look something like this: 

from pathlib import Path
import pandas as pd

os_path = Path(__file__).resolve().parent
csv_file = os_path / "AGOL_Users_ToExpire" / "UsersAboutToExpire.csv"
df_new = pd.read_csv(csv_file)


Tyler