MCPcopy Index your code
hub / github.com/ipython/ipython / filefind

Function filefind

IPython/utils/path.py:93–149  ·  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: str, path_dirs=None)

Source from the content-addressed store, hash-verified

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

Callers 1

_exec_fileMethod · 0.90

Calls 1

expand_pathFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…