Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from pathlib import Path
- from tkinter import Tk, TclError
- def get_clipboard_as_path():
- root = Tk()
- root.withdraw()
- try:
- content = root.selection_get(selection="CLIPBOARD")
- except TclError:
- return None
- finally:
- root.destroy()
- file = Path(content)
- try:
- if file.exists():
- return file
- except OSError:
- pass
- return None
- # get the path
- my_path = get_clipboard_as_path()
- # my_path could be None
- if my_path:
- print(f'The path is {my_path}')
- # if my_path is a directory, find all python files
- if my_path.is_dir():
- print('The path is a directory')
- for file in my_path.glob('*.py'):
- print(file)
- else:
- print('The path is a file')
- with my_path.open() as fd:
- header = fd.readline()
- print('First line:')
- print(header)
- else:
- print('It was not a Path, maybe a image or something else')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement