My desktop computer is running Linux Mint 19.1. Another computer is running Linux Mint 18.3. Both computers are running Samba. I am trying to have my desktop python code check for a file on the other computer. In the Caja file manager it shows the path as "smb://lenovo2/kwpvr/". I can copy, delete or rename the file in Caja.
This question was previously asked but the answer only addressed that other operating system (Windows). os.path.isfile() returns false for file on network drive
#!/usr/bin/python3
import os
print(os.path.isfile("smb://lenovo2/kwpvr/kwpvr3.db")) # False
print (os.path.isfile("//lenovo2/kwpvr/kwpvr3.db")) # False
print (os.path.isfile("smb:\\lenovo2\kwpvr\kwpvr3.db")) # False
print (os.path.isfile("\\lenovo2\kwpvr\kwpvr3.db")) # False
print (os.path.exists("smb://lenovo2/kwpvr/kwpvr3.db")) # False
print (os.path.exists("//lenovo2/kwpvr/kwpvr3.db")) # False
print (os.path.exists("smb:\\lenovo2\kwpvr\kwpvr3.db")) # False
print (os.path.exists("\\lenovo2\kwpvr\kwpvr3.db")) # False
os.path.exists() always returns False for any permutation of the file name I can come up with.
os.path
only works for files that can be accessed through the local file system. It does not work with URIs like smb://
or https://
.
If you want to make files on network shares available through the local file system on Linux, you have to mount them, e.g:
mount -t cifs //host/myshare /mnt/mypath
and then use os.path.exists("/mnt/mypath/hello.txt")
Alternatively, use a SMB/CIFS API instead of os.path
.