MCPcopy Create free account
hub / github.com/ipython/ipython / filefind

Function filefind

IPython/utils/path.py:112–163  ·  view source on GitHub ↗

Find a file by looking through a sequence of paths. This iterates through a sequence of paths looking for a file and returns the full, absolute path of the first occurrence of the file. If no set of path dirs is given, the filename is tested as is, after running through :func:`expa

(filename, path_dirs=None)

Source from the content-addressed store, hash-verified

110
111
112def filefind(filename, path_dirs=None):
113 """Find a file by looking through a sequence of paths.
114
115 This iterates through a sequence of paths looking for a file and returns
116 the full, absolute path of the first occurrence of the file. If no set of
117 path dirs is given, the filename is tested as is, after running through
118 :func:`expandvars` and :func:`expanduser`. Thus a simple call::
119
120 filefind('myfile.txt')
121
122 will find the file in the current working dir, but::
123
124 filefind('~/myfile.txt')
125
126 Will find the file in the users home directory. This function does not
127 automatically try any paths, such as the cwd or the user's home directory.
128
129 Parameters
130 ----------
131 filename : str
132 The filename to look for.
133 path_dirs : str, None or sequence of str
134 The sequence of paths to look for the file in. If None, the filename
135 need to be absolute or be in the cwd. If a string, the string is
136 put into a sequence and the searched. If a sequence, walk through
137 each element and join with ``filename``, calling :func:`expandvars`
138 and :func:`expanduser` before testing for existence.
139
140 Returns
141 -------
142 Raises :exc:`IOError` or returns absolute path to file.
143 """
144
145 # If paths are quoted, abspath gets confused, strip them...
146 filename = filename.strip('"').strip("'")
147 # If the input is an absolute path, just check it exists
148 if os.path.isabs(filename) and os.path.isfile(filename):
149 return filename
150
151 if path_dirs is None:
152 path_dirs = ("",)
153 elif isinstance(path_dirs, str):
154 path_dirs = (path_dirs,)
155
156 for path in path_dirs:
157 if path == '.': path = os.getcwd()
158 testname = expand_path(os.path.join(path, filename))
159 if os.path.isfile(testname):
160 return os.path.abspath(testname)
161
162 raise IOError("File %r does not exist in any of the search paths: %r" %
163 (filename, path_dirs) )
164
165
166class HomeDirError(Exception):

Callers 1

_exec_fileMethod · 0.90

Calls 1

expand_pathFunction · 0.85

Tested by

no test coverage detected