Paths and files =============== Remember, always import library first. .. code-block:: python import easymacro as app Get info path ------------- .. code-block:: python path = '/home/mau/myfile.ods' p = app.path(path) app.debug(p.path) app.debug(p.file_name) app.debug(p.name) app.debug(p.ext) app.debug(p.size) app.debug(p.url) .. image:: _static/images/path_01.png | Get info like tuple .. code-block:: python app.debug(p.info) .. image:: _static/images/path_02.png | Or like dict .. code-block:: python app.debug(p.dict) .. image:: _static/images/path_03.png Get home path ------------- .. code-block:: python p = app.path app.debug(p.home) Get document path ------------------ .. code-block:: python p = app.path app.debug(p.documents) Get path user profile --------------------- .. code-block:: python p = app.path app.debug(p.user_profile) Get path user config -------------------- .. code-block:: python p = app.path app.debug(p.user_config) Get python executable path -------------------------- .. code-block:: python p = app.path app.debug(p.python) Path URL to system ------------------ .. code-block:: python path = 'file:///home/mau/myfile.ods' app.debug(app.path.to_system(path)) Path system to URL ------------------ .. code-block:: python path = 'file:///home/mau/myfile.ods' path = app.path.to_system(path) app.debug(app.path.to_url(path)) Get path from user config ------------------------- Default get path documents. `See Api XPathSettings `_ .. code-block:: python path = app.path.config() app.debug(path) path = app.path.config('UserConfig') app.debug(path) .. note:: Some paths can be more than one path separated by a semicolon, in this case, you get a `list` of paths. Path join --------- .. code-block:: python path = app.path.join('/home/mau', 'test', 'file.ods') app.debug(path) Exists path ----------- .. code-block:: python exists = app.path.exists('/home/mau/test/file.ods') app.debug(exists) Verify if application exists ---------------------------- .. code-block:: python app_name = 'nosoffice' app.debug(app.path.exists_app(app_name)) app_name = 'soffice' app.debug(app.path.exists_app(app_name)) Path is file ------------ .. code-block:: python path = '/home/mau/myfile.ott' app.msgbox(app.path.is_file(path)) Path is dir ----------- .. code-block:: python path = '/home/mau' app.msgbox(app.path.is_dir(path)) Make temporary file ------------------- It will be destroyed as soon as it is closed. .. code-block:: python f = app.path.temp_file() f.write(app.NAME) f.close() If used in context, It will be destroyed too. .. code-block:: python with app.path.temp_file() as f: app.debug(f.name) f.write('test') Make temporary directory ------------------------ On completion of the context or destruction of the temporary directory object, the newly created temporary directory and all its contents are removed from the filesystem. .. code-block:: python with app.path.temp_dir() as d: app.debug(app.path.exists(d)) app.debug(d) Get path for save ----------------- Default open in user documents ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .. code-block:: python path = app.path.get() app.msgbox(path) Open in other path ^^^^^^^^^^^^^^^^^^ .. code-block:: python path_tmp = app.path.config('Temp') path = app.path.get(path_tmp) app.msgbox(path) Add one filter ^^^^^^^^^^^^^^ .. code-block:: python path = app.path.get(filters='xml') app.msgbox(path) Add multiple filters ^^^^^^^^^^^^^^^^^^^^ .. code-block:: python path = app.path.get(filters='xml,txt') Select directory ---------------- Default open in user documents ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .. code-block:: python path = app.path.get_dir() app.debug(path) Open in other path ^^^^^^^^^^^^^^^^^^ .. code-block:: python path_tmp = app.path.config('Temp') path_dir = app.paths.get_dir(path_tmp) app.debug(path_dir) Get path exists file -------------------- Default open in user documents ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .. code-block:: python path_file = app.path.get_file() app.msgbox(path_file) Change init dir ^^^^^^^^^^^^^^^ .. code-block:: python path = '/home/mau' path_file = app.path.get_file(path) app.msgbox(path_file) Add filter or filters ^^^^^^^^^^^^^^^^^^^^^ .. code-block:: python path_file = app.path.get_file(filters='ods') # or path_file = app.path.get_file(filters='ods,odt') Can select multiple files ^^^^^^^^^^^^^^^^^^^^^^^^^ .. code-block:: python path_files = app.path.get_file(multiple=True) Get files --------- Get files not recursively ^^^^^^^^^^^^^^^^^^^^^^^^^ .. code-block:: python path = '/home/mau/Documents' files = app.path.files(path) for f in files: app.debug(f) Add filter ^^^^^^^^^^ .. code-block:: python files = app.path.files(path, '*.pdf') Get content files, recursively ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .. code-block:: python files = app.path.files(path, '**/*.pdf') Get content files recursively ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ This method use `os.walk` .. code-block:: python path = '/home/mau/Documents' files = app.path.walk(path) for f in files: app.debug(f) Add filter ^^^^^^^^^^ .. code-block:: python files = app.path.walk(path, 'ods') # or filters files = app.path.walk(path, 'ods|odt') Get directories --------------- Get directories not recursively ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ This method use library `pathlib` .. code-block:: python path = '/home/mau/Documents' folders = app.path.dirs(path) for f in folders: app.debug(f) Get directories recursively ^^^^^^^^^^^^^^^^^^^^^^^^^^^ This method use `os.walk` .. code-block:: python path = '/home/mau/Documents' folders = app.path.walk_dirs(path) for f in folders: app.debug(f) Get info in a tuple ^^^^^^^^^^^^^^^^^^^ Like (ID_FOLDER, ID_PARENT, NAME) .. code-block:: python path = '/home/mau/Documents' folders = app.path.walk_dirs(path, True) for f in folders: app.debug(f) Get install path extension from id ---------------------------------- .. code-block:: python id_ext = 'net.elmau.zaz.EasyMacro' path = app.path.extension(id_ext) app.debug(path) .. code-block:: bash 24/06/2021 21:47:29 - DEBUG - /home/mau/.config/libreoffice/4/user/uno_packages/cache/uno_packages/lu20665x29msz.tmp_/ZAZEasyMacro_v0.1.0.oxt Replace extension ----------------- .. code-block:: python path = '/home/mau/myFile.ods' path_new = app.path.replace_ext(path, 'pdf') app.debug(path_new) Open any type file ------------------ Open with default application in OS. .. code-block:: python path = '/home/mau/file.pdf' app.path.open(path) path = '/home/mau/index.html' app.path.open(path) Save and read text data ----------------------- Default encoding is UTF8 .. code-block:: python data = """Do you want to know who you are? Don't ask. Act! Action will delineate and define you. Thomas Jefferson """ path = '/home/mau/temp.txt' app.path.save(path, data) data = app.path.read(path) app.msgbox(data) Change encoding .. code-block:: python app.path.save(path, data, 'iso-8859-1') Save and read binary data ------------------------- .. code-block:: python data = b'Binary data' path = '/home/mau/temp.bin' app.path.save_bin(path, data) data = app.path.read_bin(path) app.msgbox(data) Save and read json ------------------ .. code-block:: python path = '/home/mau/data.json' data = { 'type': 'calc', 'name': 'myfile.ods', } app.path.to_json(path, data) data = app.path.from_json(path) app.msgbox(data) Save and read csv ----------------- You can used the same way that `python csv`_ .. code-block:: python path = '/home/mau/data.csv' data = ( (1, 'one', app.now()), (2, 'two', app.now()), (3, 'three', app.now()), ) app.path.to_csv(path, data) data = app.path.from_csv(path) app.msgbox(data) Delete files and directories ---------------------------- **CAUTION**: This method delete files and directories without confirmation, always ask to user first. .. code-block:: python path = '/home/mau/temp.bin' result = app.path.kill(path) app.msgbox(result) Delete directory and all content ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .. code-block:: python path = '/home/mau/safe_for_delete' result = app.path.kill(path) app.msgbox(result) .. _python csv: https://docs.python.org/3.7/library/csv.html