Xfce Forum

Sub domains
 

You are not logged in.

#1 2023-05-19 20:04:29

D.dave
Member
Registered: 2019-12-06
Posts: 58

Python script to get file path: insert the result inside quotes.

Hi,

I have a python script which copy the full path of text files opened in gedit:

#!/usr/bin/env python3

import subprocess
import sys

name = subprocess.check_output(["xdotool", "getactivewindow", "getwindowname"]).decode("utf-8").strip()
if all(["(" in name, ")" in name]):
    path = name[name.find("(")+1:name.find(")")]
    if sys.argv[1] == "-file":
        fname = name[:name.find("(")]
    elif sys.argv[1] == "-path":
        fname = ""
    command = "echo "+'"'+path+"/"+fname+'"'+" | tr -d '\n' | sed 's/.$//' | xclip -selection clipboard"
    subprocess.Popen(["/bin/bash", "-c", command])

Works as expected, but I'd like to have the result reported inside quotes:

"path/to/file"

I've tried various attempts using sed, but the code looks "ugly", "strange" and doesn't work, maybe because this part

command = "echo "+'"'+path+"/"+fname+'"'+" | tr -d '\n' | sed 's/.$//' | xclip -selection clipboard"

already have various quotation, and adding quotes to the code cause a mess.

How to achieve in the best way?

Offline

#2 2023-05-19 23:28:05

asher
Member
Registered: 2023-05-19
Posts: 3

Re: Python script to get file path: insert the result inside quotes.

I don't know exactly this is not a native language that I use but maybe you will like this

#!/usr/bin/env python3

import subprocess
import sys

name = subprocess.check_output(["xdotool", "getactivewindow", "getwindowname"]).decode("utf-8").strip()
if all(["(" in name, ")" in name]):
    path = name[name.find("(") + 1:name.find(")")]
    if sys.argv[1] == "-file":
        fname = name[:name.find("(")]
    elif sys.argv[1] == "-path":
        fname = ""
    command = 'echo "' + path + '/' + fname + '" | tr -d "\n" | sed "s/.$//" | xclip -selection clipboard'
    subprocess.Popen(["/bin/bash", "-c", command])

Offline

#3 2023-05-20 07:08:08

D.dave
Member
Registered: 2019-12-06
Posts: 58

Re: Python script to get file path: insert the result inside quotes.

asher wrote:

#!/usr/bin/env python3

import subprocess
import sys

name = subprocess.check_output(["xdotool", "getactivewindow", "getwindowname"]).decode("utf-8").strip()
if all(["(" in name, ")" in name]):
    path = name[name.find("(") + 1:name.find(")")]
    if sys.argv[1] == "-file":
        fname = name[:name.find("(")]
    elif sys.argv[1] == "-path":
        fname = ""
    command = 'echo "' + path + '/' + fname + '" | tr -d "\n" | sed "s/.$//" | xclip -selection clipboard'
    subprocess.Popen(["/bin/bash", "-c", command])

Hi, unfortunately doesn't work:  the path is still reported without quotes.

Offline

#4 2023-05-20 17:41:00

D.dave
Member
Registered: 2019-12-06
Posts: 58

Re: Python script to get file path: insert the result inside quotes.

I solved with a bash script:

#!/bin/bash

path=$(xdotool getactivewindow getwindowname | grep -oP '\(\K[^)]+')
path2="${path/#\~/$HOME}"
filename=$(xdotool getactivewindow getwindowname | cut -d"(" -f1 | rev | cut -c2- | rev)


echo "\"$path2/$filename"\" | tr -d '\n' | xclip -selection clipboard

And so, I got, eg: "/home/dave/Documents/text file"

Last edited by D.dave (2023-05-20 17:41:10)

Offline

#5 2023-05-20 17:41:22

jaywilkas
Member
Registered: 2021-06-05
Posts: 19

Re: Python script to get file path: insert the result inside quotes.

Here a simple example for the command "date" and how to print its output in quotes:

import subprocess                                                                    
import sys                                                                           

command = "date"                                                                     
output_bytes = subprocess.Popen(["/bin/bash", "-c", command], stdout=subprocess.PIPE).stdout.read()
output = output_bytes.decode().strip()                                               
print(f'"{output}"')

Offline

Registered users online in this topic: 0, guests: 1
[Bot] ClaudeBot

Board footer

Powered by FluxBB
Modified by Visman

[ Generated in 0.014 seconds, 9 queries executed - Memory usage: 533.62 KiB (Peak: 534.24 KiB) ]