How do i open a python path?
Suppose my python code is executed a directory called how should I use I found that below simple code will work.. does it have any disadvantages?
Neuron 4,5854 gold badges32 silver badges53 bronze badges asked Aug 23, 2011 at 18:24 4 With this type of thing you need to be careful what your actual working directory is. For example, you may not run the script from the directory the file is in. In this case, you can't just use a relative path by itself. If you are sure the file you want is in a subdirectory beneath where the script is actually located, you can use So you can fiddle with something like this:
answered Aug 23, 2011 at 18:59
RussRuss 10.3k12 gold badges41 silver badges55 bronze badges 6
This code works fine:
Neuron 4,5854 gold badges32 silver badges53 bronze badges answered Oct 6, 2015 at 15:06
4 I created an account just so I could clarify a discrepancy I think I found in Russ's original response. For reference, his original answer was:
This is a great answer because it is trying to dynamically creates an absolute system path to the desired file. Cory Mawhorter noticed that To use this method (and how I eventually got it working) you have to remove the script name from the end of the path:
The resulting abs_file_path (in this example) becomes:
Ferguzz 5,4875 gold badges31 silver badges41 bronze badges answered Sep 1, 2014 at 20:54
2 It depends on what operating system you're using. If you want a solution that is compatible with both Windows and *nix something like:
should work fine. The Edit: As mentioned by kindall in the comments, python can convert between unix-style and windows-style paths anyway, so even simpler code will work:
That being said, the answered Aug 23, 2011 at 18:30
WilduckWilduck 13.5k10 gold badges57 silver badges89 bronze badges 5
I spend a lot time to discover why my code could not find my file running Python 3 on the Windows system. So I added . before / and everything worked fine:
answered Sep 1, 2018 at 22:14
Ângelo PolottoÂngelo Polotto 6,8652 gold badges31 silver badges35 bronze badges 4 Try this:
Python 3.4 introduced a new standard library for dealing with files and paths called pathlib. It works for me! answered Aug 3, 2018 at 10:57
dcamposdcampos 1291 silver badge6 bronze badges 1 Code:
Explanation: Import library:
Use
Separates the script path into multiple items:
Remove the last item in the list (the actual script file):
Add the relative file's path:
Join the list items, and addition the relative path's file:
Now you are set to do whatever you want with the file, such as, for example:
Mr_and_Mrs_D 30.3k37 gold badges173 silver badges349 bronze badges answered Dec 22, 2018 at 22:09
Patriots299Patriots299 3553 silver badges14 bronze badges 1
answered Dec 8, 2019 at 15:02
WebucatorWebucator 2,11722 silver badges32 bronze badges If the file is in your parent folder, eg. follower.txt, you can simply use answered Dec 30, 2017 at 15:04
Olivia LiaoOlivia Liao 3653 silver badges7 bronze badges 0 Python just passes the filename you give it to the operating system, which opens it. If your operating system supports relative paths like You may find that the easiest way to answer a question like this is to try it and see what happens. answered Aug 23, 2011 at 18:31
kindallkindall 172k34 gold badges268 silver badges300 bronze badges 3 Not sure if this work everywhere. I'm using ipython in ubuntu. If you want to read file in current folder's sub-directory:
your script is in current-folder simply try this:
answered Aug 21, 2014 at 2:20
SurviSurvi 334 bronze badges Get the path of the parent folder, then
The same thing with
answered Jun 12, 2021 at 11:07
James WongJames Wong 371 silver badge5 bronze badges 2 When I was a beginner I found these
descriptions a bit intimidating. As at first I would try
and this would raise an It's because for path to be read in Unicode you simple add a
And now it works just add answered Jun 24, 2020 at 19:18
1 In Python 3.4 (PEP 428) the
The
answered Dec 13, 2021 at 20:14
NeuronNeuron 4,5854 gold badges32 silver badges53 bronze badges How do I open a file path?To view the full path of an individual file:. Click the Start button and then click Computer, click to open the location of the desired file, hold down the Shift key and right-click the file.. On the menu, there are two options to choose from that will allow you to either copy or view the entire file path:. How do I run a path in Python Windows?PATH variable. Right-clicking This PC and going to Properties.. Clicking on the Advanced system settings in the menu on the left.. Clicking on the Environment Variables button on the bottom right.. In the System variables section, selecting the Path variable and clicking on Edit.. |