get file path with python code while .absolute() not fit my answer
Question:
I’m using Linux system, extract USB file path using python code.
extract USB path only
from
sdb1 8:17 1 3.8G 0 part /media/joy/DATA
sdg1 8:97 1 29.4G 0 part /media/joy/DATA2
sdh1 8:113 1 1.9G 0 part /media/joy/DATA1
to my expect output:
/media/joy/DATA
/media/joy/DATA2
/media/joy/DATA1
.absolute(file_name)
is getting /home/joy/file_name
which is not what I want as /media/joy/DATA
The way I came up ,
I tried to called the linux command lsblk
in paython , and use regular expression with key word "/media/joy" to filter the desire path, somehow I’m stock
tried code:
import os
import subprocess
a = subprocess.check_output(['lsblk'])
str_a = str(a)
# I split by "n" cause that in erery line of str_a
str_a_splited = str_a.split("n")
USB_info = [s for s in str_a_splited if "/media/joy" in s]
print("= = = = ")
print(USB_info)
and here is the output from tried code:
= = = =
["b'NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS\nloop0 7:0 0 4K 1 loop /snap/bare/5\nloop1 7:1 0 45.9M 1 loop /snap/snap-store/582\nloop2 7:2 0 62M 1 loop /snap/core20/1587\nloop3 7:3 0 114.9M 1 loop /snap/core/13741\nloop4 7:4 0 729.4M 1 loop /snap/pycharm-professional/302\nloop5 7:5 0 284K 1 loop /snap/snapd-desktop-integration/14\nloop6 7:6 0 48M 1 loop /snap/snapd/16778\nloop7 7:7 0 346.3M 1 loop /snap/gnome-3-38-2004/115\nloop8 7:8 0 91.7M 1 loop /snap/gtk-common-themes/1535\nloop9 7:9 0 55.6M 1 loop /snap/core18/2566\nloop10 7:10 0 140.8M 1 loop /snap/skype/226\nloop11 7:11 0 63.2M 1 loop /snap/core20/1623\nloop12 7:12 0 115M 1 loop /snap/core/13886\nloop13 7:13 0 45.9M 1 loop /snap/snap-store/599\nloop14 7:14 0 238.1M 1 loop /snap/firefox/1918\nloop15 7:15 0 237.3M 1 loop /snap/firefox/1883\nloop16 7:16 0 37.1M 1 loop /snap/hunspell-dictionaries-1-7-2004/2\nloop17 7:17 0 48M 1 loop /snap/snapd/17029\nloop18 7:18 0 164.8M 1 loop /snap/gnome-3-28-1804/161\nloop19 7:19 0 346.3M 1 loop /snap/gnome-3-38-2004/119\nsda 8:0 0 931.5G 0 disk \n\xe2\x94\x9c\xe2\x94\x80sda1 8:1 0 1M 0 part \n\xe2\x94\x9c\xe2\x94\x80sda2 8:2 0 513M 0 part /boot/efi\n\xe2\x94\x94\xe2\x94\x80sda3 8:3 0 931G 0 part /var/snap/firefox/common/host-hunspell\n /\nsdb 8:16 1 3.8G 0 disk \n\xe2\x94\x94\xe2\x94\x80sdb1 8:17 1 3.8G 0 part /media/joy/DATA\nsdc 8:32 1 0B 0 disk \nsdd 8:48 1 0B 0 disk \nsde 8:64 1 0B 0 disk \nsdf 8:80 1 0B 0 disk \nsdg 8:96 1 29.4G 0 disk \n\xe2\x94\x94\xe2\x94\x80sdg1 8:97 1 29.4G 0 part /media/joy/DATA2\nsdh 8:112 1 1.9G 0 disk \n\xe2\x94\x94\xe2\x94\x80sdh1 8:113 1 1.9G 0 part /media/joy/DATA1\nsr0 11:0 1 1024M 0 rom \nsr1 11:1 1 1024M 0 rom \n'"]
the linux command lsblk
:
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
loop0 7:0 0 4K 1 loop /snap/bare/5
loop1 7:1 0 45.9M 1 loop /snap/snap-store/582
loop2 7:2 0 62M 1 loop /snap/core20/1587
loop3 7:3 0 114.9M 1 loop /snap/core/13741
loop4 7:4 0 729.4M 1 loop /snap/pycharm-professional/302
loop5 7:5 0 284K 1 loop /snap/snapd-desktop-integration/14
loop6 7:6 0 48M 1 loop /snap/snapd/16778
loop7 7:7 0 346.3M 1 loop /snap/gnome-3-38-2004/115
loop8 7:8 0 91.7M 1 loop /snap/gtk-common-themes/1535
loop9 7:9 0 55.6M 1 loop /snap/core18/2566
loop10 7:10 0 140.8M 1 loop /snap/skype/226
loop11 7:11 0 63.2M 1 loop /snap/core20/1623
loop12 7:12 0 115M 1 loop /snap/core/13886
loop13 7:13 0 45.9M 1 loop /snap/snap-store/599
loop14 7:14 0 238.1M 1 loop /snap/firefox/1918
loop15 7:15 0 237.3M 1 loop /snap/firefox/1883
loop16 7:16 0 37.1M 1 loop /snap/hunspell-dictionaries-1-7-2004/2
loop17 7:17 0 48M 1 loop /snap/snapd/17029
loop18 7:18 0 164.8M 1 loop /snap/gnome-3-28-1804/161
loop19 7:19 0 346.3M 1 loop /snap/gnome-3-38-2004/119
sda 8:0 0 931.5G 0 disk
├─sda1 8:1 0 1M 0 part
├─sda2 8:2 0 513M 0 part /boot/efi
└─sda3 8:3 0 931G 0 part /var/snap/firefox/common/host-hunspell
/
sdb 8:16 1 3.8G 0 disk
└─sdb1 8:17 1 3.8G 0 part /media/joy/DATA
sdc 8:32 1 0B 0 disk
sdd 8:48 1 0B 0 disk
sde 8:64 1 0B 0 disk
sdf 8:80 1 0B 0 disk
sdg 8:96 1 29.4G 0 disk
└─sdg1 8:97 1 29.4G 0 part /media/joy/DATA2
sdh 8:112 1 1.9G 0 disk
└─sdh1 8:113 1 1.9G 0 part /media/joy/DATA1
sr0 11:0 1 1024M 0 rom
sr1 11:1 1 1024M 0 rom
Answers:
I changed your code a little and it works now as you expected:
import os
import subprocess
a = subprocess.check_output(['lsblk'])
# I split by "n" cause that in erery line of str_a
str_a_splited = a.split("n")
USB_info = [s for s in str_a_splited if "/media/" in s]
found_path = []
for x in USB_info:
for s in x.split(" "):
if "/media/" in s:
found_path.append(s)
print("= = = = ")
print(found_path)
and you don’t need to convert the output of subprocess
to str
since it is in str
format by default.I tested this on my PC and worked fine.
I add .decode()
in case TypeError pop out
import os
import subprocess
a = subprocess.check_output(['lsblk'])
# I split by "n" cause that in erery line of str_a
str_a_splited = a.decode().split("n")
USB_info = [s for s in str_a_splited if "/media/" in s]
found_path = []
for x in USB_info:
for s in x.split(" "):
if "/media/" in s:
found_path.append(s)
print("= = = = ")
print(found_path)
I’m using Linux system, extract USB file path using python code.
extract USB path only
from
sdb1 8:17 1 3.8G 0 part /media/joy/DATA
sdg1 8:97 1 29.4G 0 part /media/joy/DATA2
sdh1 8:113 1 1.9G 0 part /media/joy/DATA1
to my expect output:
/media/joy/DATA
/media/joy/DATA2
/media/joy/DATA1
.absolute(file_name)
is getting /home/joy/file_name
which is not what I want as /media/joy/DATA
The way I came up ,
I tried to called the linux command lsblk
in paython , and use regular expression with key word "/media/joy" to filter the desire path, somehow I’m stock
tried code:
import os
import subprocess
a = subprocess.check_output(['lsblk'])
str_a = str(a)
# I split by "n" cause that in erery line of str_a
str_a_splited = str_a.split("n")
USB_info = [s for s in str_a_splited if "/media/joy" in s]
print("= = = = ")
print(USB_info)
and here is the output from tried code:
= = = =
["b'NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS\nloop0 7:0 0 4K 1 loop /snap/bare/5\nloop1 7:1 0 45.9M 1 loop /snap/snap-store/582\nloop2 7:2 0 62M 1 loop /snap/core20/1587\nloop3 7:3 0 114.9M 1 loop /snap/core/13741\nloop4 7:4 0 729.4M 1 loop /snap/pycharm-professional/302\nloop5 7:5 0 284K 1 loop /snap/snapd-desktop-integration/14\nloop6 7:6 0 48M 1 loop /snap/snapd/16778\nloop7 7:7 0 346.3M 1 loop /snap/gnome-3-38-2004/115\nloop8 7:8 0 91.7M 1 loop /snap/gtk-common-themes/1535\nloop9 7:9 0 55.6M 1 loop /snap/core18/2566\nloop10 7:10 0 140.8M 1 loop /snap/skype/226\nloop11 7:11 0 63.2M 1 loop /snap/core20/1623\nloop12 7:12 0 115M 1 loop /snap/core/13886\nloop13 7:13 0 45.9M 1 loop /snap/snap-store/599\nloop14 7:14 0 238.1M 1 loop /snap/firefox/1918\nloop15 7:15 0 237.3M 1 loop /snap/firefox/1883\nloop16 7:16 0 37.1M 1 loop /snap/hunspell-dictionaries-1-7-2004/2\nloop17 7:17 0 48M 1 loop /snap/snapd/17029\nloop18 7:18 0 164.8M 1 loop /snap/gnome-3-28-1804/161\nloop19 7:19 0 346.3M 1 loop /snap/gnome-3-38-2004/119\nsda 8:0 0 931.5G 0 disk \n\xe2\x94\x9c\xe2\x94\x80sda1 8:1 0 1M 0 part \n\xe2\x94\x9c\xe2\x94\x80sda2 8:2 0 513M 0 part /boot/efi\n\xe2\x94\x94\xe2\x94\x80sda3 8:3 0 931G 0 part /var/snap/firefox/common/host-hunspell\n /\nsdb 8:16 1 3.8G 0 disk \n\xe2\x94\x94\xe2\x94\x80sdb1 8:17 1 3.8G 0 part /media/joy/DATA\nsdc 8:32 1 0B 0 disk \nsdd 8:48 1 0B 0 disk \nsde 8:64 1 0B 0 disk \nsdf 8:80 1 0B 0 disk \nsdg 8:96 1 29.4G 0 disk \n\xe2\x94\x94\xe2\x94\x80sdg1 8:97 1 29.4G 0 part /media/joy/DATA2\nsdh 8:112 1 1.9G 0 disk \n\xe2\x94\x94\xe2\x94\x80sdh1 8:113 1 1.9G 0 part /media/joy/DATA1\nsr0 11:0 1 1024M 0 rom \nsr1 11:1 1 1024M 0 rom \n'"]
the linux command lsblk
:
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
loop0 7:0 0 4K 1 loop /snap/bare/5
loop1 7:1 0 45.9M 1 loop /snap/snap-store/582
loop2 7:2 0 62M 1 loop /snap/core20/1587
loop3 7:3 0 114.9M 1 loop /snap/core/13741
loop4 7:4 0 729.4M 1 loop /snap/pycharm-professional/302
loop5 7:5 0 284K 1 loop /snap/snapd-desktop-integration/14
loop6 7:6 0 48M 1 loop /snap/snapd/16778
loop7 7:7 0 346.3M 1 loop /snap/gnome-3-38-2004/115
loop8 7:8 0 91.7M 1 loop /snap/gtk-common-themes/1535
loop9 7:9 0 55.6M 1 loop /snap/core18/2566
loop10 7:10 0 140.8M 1 loop /snap/skype/226
loop11 7:11 0 63.2M 1 loop /snap/core20/1623
loop12 7:12 0 115M 1 loop /snap/core/13886
loop13 7:13 0 45.9M 1 loop /snap/snap-store/599
loop14 7:14 0 238.1M 1 loop /snap/firefox/1918
loop15 7:15 0 237.3M 1 loop /snap/firefox/1883
loop16 7:16 0 37.1M 1 loop /snap/hunspell-dictionaries-1-7-2004/2
loop17 7:17 0 48M 1 loop /snap/snapd/17029
loop18 7:18 0 164.8M 1 loop /snap/gnome-3-28-1804/161
loop19 7:19 0 346.3M 1 loop /snap/gnome-3-38-2004/119
sda 8:0 0 931.5G 0 disk
├─sda1 8:1 0 1M 0 part
├─sda2 8:2 0 513M 0 part /boot/efi
└─sda3 8:3 0 931G 0 part /var/snap/firefox/common/host-hunspell
/
sdb 8:16 1 3.8G 0 disk
└─sdb1 8:17 1 3.8G 0 part /media/joy/DATA
sdc 8:32 1 0B 0 disk
sdd 8:48 1 0B 0 disk
sde 8:64 1 0B 0 disk
sdf 8:80 1 0B 0 disk
sdg 8:96 1 29.4G 0 disk
└─sdg1 8:97 1 29.4G 0 part /media/joy/DATA2
sdh 8:112 1 1.9G 0 disk
└─sdh1 8:113 1 1.9G 0 part /media/joy/DATA1
sr0 11:0 1 1024M 0 rom
sr1 11:1 1 1024M 0 rom
I changed your code a little and it works now as you expected:
import os
import subprocess
a = subprocess.check_output(['lsblk'])
# I split by "n" cause that in erery line of str_a
str_a_splited = a.split("n")
USB_info = [s for s in str_a_splited if "/media/" in s]
found_path = []
for x in USB_info:
for s in x.split(" "):
if "/media/" in s:
found_path.append(s)
print("= = = = ")
print(found_path)
and you don’t need to convert the output of subprocess
to str
since it is in str
format by default.I tested this on my PC and worked fine.
I add .decode()
in case TypeError pop out
import os
import subprocess
a = subprocess.check_output(['lsblk'])
# I split by "n" cause that in erery line of str_a
str_a_splited = a.decode().split("n")
USB_info = [s for s in str_a_splited if "/media/" in s]
found_path = []
for x in USB_info:
for s in x.split(" "):
if "/media/" in s:
found_path.append(s)
print("= = = = ")
print(found_path)