Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python examples: pathlib & avoid chdir #10

Open
snowman2 opened this issue Nov 5, 2021 · 2 comments
Open

Python examples: pathlib & avoid chdir #10

snowman2 opened this issue Nov 5, 2021 · 2 comments

Comments

@snowman2
Copy link

snowman2 commented Nov 5, 2021

https://www.earthdatascience.org/courses/use-data-open-source-python/multispectral-remote-sensing/intro-naip/

I think it is better to build your path instead of os.chdir to prevent confusion. This is my opinion, so feel free to ignore if you disagree.

Also, the examples could benefit from using pathlib.

@lwasser
Copy link

lwasser commented Nov 8, 2021

sure @snowman2 i think i follow. The lessons in our textbook are focused on newer-ish users - this is an intermediate book. The idea behind this:

# Set working directory
os.chdir(os.path.join(et.io.HOME, 'earth-analytics', 'data'))

was that we'd have then always save data in their home/earth-analytics/data directory and then call the data from there. Are you suggesting rather than changing the directory we simply build paths like this:

home_path - os.path.join(et.io.HOME, 'earth-analytics', 'data')

OR using pathlib instead could work to generate a path to their home directory potentially. and then calling data from that explicit path?

# or do this with object oriented pathlib rather than just os
data_path = os.path.join(home_path, path-to-data-here)

thank you. i want to clarify because all of our lessons use this approach of changing the directory so it would be a big overhaul BUT i think i understand your point - changing the working dir makes it work on all machines but could be problematic in some scenarios.

Any clarification is helpful here. i appreciate it!

@snowman2
Copy link
Author

snowman2 commented Nov 8, 2021

Thanks for asking clarification 👍 . I am suggesting to do:

from pathlib import Path

work_dir = Path(et.io.HOME, 'earth-analytics', 'data')

And then when writing files to disk:

rds.rio.to_raster(str(work_dir / "filename.tif"))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants