I‘d like to write a Python program which first detects a new USB disk with Terminal on macOS, then returns me the full path to the stick.
I‘ve tried to implement it like that:
JavaScript
x
4
1
os.chdir(‘/Volumes‘)
2
#then do some listing
3
List = os.listdir()
4
But this returns me just
JavaScript
1
3
1
My_USB_Stick
2
Macintosh HD
3
I have no idea how to get the path of the connected drive, which excludes Macintosh HD…
Any Ideas ? Looking forward to hearing from you ;)
My_USB
Advertisement
Answer
One way to to it would be:
- Find a way to list the complete path of all files while performing the
ls
command in python - Loop through the list and exclude the
Macintosh HD
file.
There is another easier way to implement the same. You could loop over the List
and add the current path (/Volumes/
) to the files/folders in the list. Here is an implementation of the same:
JavaScript
1
14
14
1
import os
2
os.chdir('/Volumes')
3
# then do some listing
4
List = os.listdir()
5
i = 0
6
while i < len(List):
7
if (List[i] == 'Macintosh HD'):
8
del List[i:i+1]
9
continue
10
else:
11
List[i] = '/Volumes/' + List[i]
12
i += 1
13
print(List)
14