You should probably move the first paragraph about whether this is good form to a comment on your answer, or at the very least to a parenthetical at the end of the answer, instead of being at the very top. But otherwise, there's nothing wrong with your form. Most importantly, it's a good answer that will be useful to anyone with this problem, and you even expanded it from your original comment. And you're not trying to steal anyone else's credit or game the system.
So… what could be wrong with it? Thanks abarnert. I've never done it before so I thought I'd check. It didn't seem to benefit anyone else; I hadn't thought of visitors from the future hi future people! Always remember Guido's time machine. Sign up or log in Sign up using Google. Sign up using Facebook.
Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Podcast Making Agile work for data science. Stack Gives Back Featured on Meta. New post summary designs on greatest hits now, everywhere else eventually.
Related Hot Network Questions. Question feed. EDIT: This question pertains particularly to a Windows machine, as I don't know how drive letters and backslashes would factor into this.
If you know the full path to the file you can just do something similar to this. However if you question directly relates to relative paths, that I am unfamiliar with and would have to research and test. Here is a way to do it relatively instead of absolute. Not sure if this works on windows, you will have to test it. Its a very old question but I think it will help newbies line me who are learning python.
If you have Python 3. To use it, you just pass a path or filename into a new Path object using forward slashes and it handles the rest. To indicate that the path is a raw string, put r in front of the string with your actual path. Source: The easy way to deal with file paths on Windows, Mac and Linux. How are we doing? Please help us improve Stack Overflow. Take our short survey.
Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. The with keyword will also ensure that your resources get closed properly, even if you get some nasty Exception. How are we doing? Please help us improve Stack Overflow. Take our short survey. Stack Overflow for Teams — Collaborate and share knowledge with a private group.
Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Open file in a relative location in Python Ask Question. Asked 10 years, 4 months ago. Active 30 days ago. Viewed k times. I found that below simple code will work..
Improve this question. Neuron 4, 4 4 gold badges 27 27 silver badges 48 48 bronze badges. You're using unescaped backslashes. That's one disadvantage. Several disadvantages. Your string won't work. Also, 2 you are using getcwd which is the path you were in when you execute the script.
I thought you wanted relative to the script location but now am wondering. And 3 , always use os. Your path joining line should be os. And for good measure See here for the best explanation of with statements I've seen. Add a comment. Active Oldest Votes.
0コメント