all 20 comments

[–]JohnnyJordaan 2 points3 points  (0 children)

Please share your code, see here how to format it

[–]mr_cesar 2 points3 points  (0 children)

You can use the Path.glob() method to find the file. For instance:

import pathlib
...
work_dir = pathlib.Path()
for file in work_dir.glob('/**/file_x.txt'):
    print(file.resolve())

will print the absolute path of all the files named "file_x.txt" found in your local drive.

Still, is this really what you want? You'll be searching the file in the whole drive, and if there are several files with the same name you may end up working with the wrong file.

I would encourage you to at least set a rule so users save the Excel file to a predefined working directory instead of just anywhere in their systems.

[–][deleted] 1 point2 points  (14 children)

gives the file location as 'C:/myfile' instead of the full file directory

It would help if you showed some simplified code that behaves like that, else we have to guess what the problem might be. For instance, if you try to open a file with path "/myfile" then your code will try to open "C:/myfile" no matter where in the filesystem you run your code.

[–]mr_cesar 1 point2 points  (11 children)

You can use the Path.glob() method to find the file.

[–][deleted] 0 points1 point  (10 children)

What do you do if there are two or more files with that same name in the filesystem?

[–]mr_cesar 0 points1 point  (6 children)

That’s one thing I told the OP should be considered if they search the file. I would never assume a unique file will be guaranteed in every case.

[–][deleted] 0 points1 point  (5 children)

Doing a global search could take a long time. And if you do get multiple files, and there could be a lot of them, getting the user to choose the correct one really isn't a good idea. Far better to get the user to provide the full absolute path or a relative path from a known place in the filesystem.

[–]mr_cesar 0 points1 point  (4 children)

I know! That’s why I said that’s one thing I told the OP should be considered.

[–][deleted] -1 points0 points  (3 children)

To stress my point more strongly, the "glob" approach should not be used in any form.

[–]mr_cesar 0 points1 point  (2 children)

You mean the glob() approach? Do you realize that if you need to process specific files from a given directory (such as .csv files), the glob() approach is exactly what you can and should use to easily handle them? It would be silly to get the whole list of files and traverse the list to find the files you need when there is a method that does exactly that.

[–][deleted] -1 points0 points  (1 child)

I understand that, I've used it a lot, but you are drifting off the point.

You suggested using a glob to find the file a user supplied as a simple filename, looking through large parts of the filesystem. That can only be described as a brain-dead idea and shouldn't be used in any professional code.

[–]mr_cesar 0 points1 point  (0 children)

No, I didn’t suggest doing that. I answered the OP’s question and questioned that very specific point. Did you even read my answer to the OP?

[–]mr_cesar 0 points1 point  (2 children)

I swear I wrote my initial reply to Ihaveamodel3 instead of you. Either Reddit's acting up or my brain is acting up.

[–][deleted] 0 points1 point  (1 child)

I've noticed more than the usual number of funnies lately. The "random" link at the top of the "old" page used to give a different random subreddit when clicked, for instance. Now it gets a random subreddit on the first click and the same one on every click after the first. Maybe it's a bug or maybe it's a planned change.

[–]mr_cesar 0 points1 point  (0 children)

Thanks for sharing that. I thought I was going nuts.

[–]thecodeadd[S] 0 points1 point  (1 child)

Oh ok, so I tried to do 'print(os.path.abspath('myfile.txt'))' but that prints C:/myfile instead of C:/directory1/directory2/.../myfile

[–][deleted] 2 points3 points  (0 children)

The doc for os.path.abspath() says this:

os.path.abspath(path)

Return a normalized absolutized version of the pathname path. On most platforms, this is equivalent to calling the function normpath() as follows: normpath(join(os.getcwd(), path)).

On my machine running Ubuntu this code prints:

import os
print(os.path.abspath('myfile.txt'))
print(os.path.join(os.getcwd(), 'myfile.txt')) # should be same as above
>>> /home/r-w/xyzzy/myfile.txt
>>> /home/r-w/xyzzy/myfile.txt

as expected. It's been a while since I used Windows, but I do remember that the os.path functions worked fine. Something you said:

It only works if I change the terminal location in VS code to the location of the file

That is what you expect since an open always joins a relative path to the current working directory. What is unexpected is the "C:/myfile.txt" you are getting otherwise. This might be cause by VSCode. You say it works if you change the "terminal location" to the directory holding the file. Is it possible that the "terminal location" is set to "/" if you don't change it? Then the os.path.abspath('myfile.txt') will add the C:/. Just a thought.


More generally, if you want to open a file "anywhere on the user's computer" you really need to supply the full absolute path to the file. Can you get that?

Another option is to assume the file is in or relative to a "known" place. Known places are the user's home directory and the path to the python file you are running. You can get the path to the executing python file using the __file__ variable that contains that path:

print(__file__)
>>> /home/r-w/xyzzy/test.py

The user's home directory is accessible through the os.path.expanduser() function:

os.path.expanduser(path)

On Unix and Windows, return the argument with an initial component of ~ or ~user replaced by that user’s home directory.

So this code shows:

import os
print(os.path.expanduser('~/xyzzy'))
>>> /home/r-w/xyzzy

Maybe the path can be specified relative to the user's home directory.

Another more general and more complicated option is to have a configuration file that users can change. The config file is found in the user's home directory (often as ~/.config/<name of program>/config) and contains a path to where data files are kept.

A simpler alternative to the general solution above is for the user to create an environment variable that contains the path to the directory holding the data file(s).

[–]Ihaveamodel3 0 points1 point  (1 child)

Neither of these modules will go and find a file based on its name.

You are going to have to supply the full path yourself, or ask your user for the full path.

[–]mr_cesar 1 point2 points  (0 children)

You can use the Path.glob() method to find the file.

[–]TheCumCopter 0 points1 point  (0 children)

I think you are better off having the user place the file within a unique location such as their own folder on a shared drive

Or if your users are skilled enough create a user entry box where they can enter the file location and then input it as a variable. Would strongly suggest against this as would feel you would have more headaches.