Mon, 06 Mar 2023 11:39:26 +0100
Added a method to check the existence of a file or directory on a MicroPython device.
src/eric7/MicroPython/Devices/DeviceBase.py | file | annotate | diff | comparison | revisions | |
src/eric7/MicroPython/MicroPythonFileManager.py | file | annotate | diff | comparison | revisions |
--- a/src/eric7/MicroPython/Devices/DeviceBase.py Mon Mar 06 09:50:18 2023 +0100 +++ b/src/eric7/MicroPython/Devices/DeviceBase.py Mon Mar 06 11:39:26 2023 +0100 @@ -34,7 +34,8 @@ commands are: <ul> <li>cd: change directory</li> - <li>fileSystemInfo: get information about the file system + <li>exists: test the existence of a file or directory on the device</li> + <li>fileSystemInfo: get information about the file system</li> <li>get: get a file from the connected device</li> <li>getData: read data of a file of the connected device</li> <li>lls: directory listing with meta data</li> @@ -45,7 +46,7 @@ <li>pwd: get the current directory</li> <li>rm: remove a file from the connected device</li> <li>rmdir: remove an empty directory</li> - <li>rmrf: remove a file/directory recursively (like 'rm -rf' in bash) + <li>rmrf: remove a file/directory recursively (like 'rm -rf' in bash)</li> </ul> Supported non file system commands are: @@ -478,6 +479,32 @@ ## Methods below implement the file system commands ################################################################## + def exists(self, pathname): + """ + Public method to check the existence of a file or directory. + + @param pathname name of the path to check + @type str + @return flag indicating the existence + @rtype bool + @exception OSError raised to indicate an issue with the device + """ + command = """ +import os as __os_ +try: + __os_.stat({0}) + print(True) +except OSError: + print(False) +del __os_ +""".format( + repr(pathname) + ) + out, err = self._interface.execute(command, mode=self._submitMode) + if err: + raise OSError(self._shortError(err)) + return out.strip() == b"True" + def ls(self, dirname=""): """ Public method to get a directory listing of the connected device.
--- a/src/eric7/MicroPython/MicroPythonFileManager.py Mon Mar 06 09:50:18 2023 +0100 +++ b/src/eric7/MicroPython/MicroPythonFileManager.py Mon Mar 06 11:39:26 2023 +0100 @@ -81,6 +81,21 @@ self.__device = device + def exists(self, pathname): + """ + Public method to check the existence of a file or directory. + + @param pathname name of the path to check + @type str + @return flag indicating the existence + @rtype bool + """ + try: + return self.__device.exists(pathname) + except Exception as exc: + self.error.emit("exists", str(exc)) + return False + @pyqtSlot(str) def lls(self, dirname, showHidden=False): """