Mac及Ubuntu环境下Python连接Windows共享路径并检索指定文件
Got it, let's break this down. The reason os.listdir isn't working is that Python can't natively interpret SMB (smb://) paths as filesystem directories. You first need to mount the Windows share to your local machine's filesystem—once that's done, Python can treat it like any regular folder. Here's how to set this up for both environments, plus the code to hunt down those .abc.html files.
Mac (Development Environment)
Step 1: Mount the Windows Share
You can do this via terminal (more scriptable for automation) or Finder:
- Terminal method: Use
mount_smbfsto mount the share to a local mount point (create the mount point first if it doesn't exist):
If you don't want to put your password in plaintext, omit it from the command—you'll be prompted to enter it securely.# Create a mount point (e.g., in /Volumes) mkdir -p /Volumes/WinShare # Mount the share (replace placeholders with your actual credentials and server path) mount_smbfs //your_username:your_password@windows-server-name/share-folder /Volumes/WinShare - Finder method: You already know this, but just to confirm: Go to
Go > Connect to Server, entersmb://windows-server-name/share-folder, log in, and the share will appear in/Volumes.
Step 2: Python Code to Find .abc.html Files
Once mounted, use os.walk or pathlib (modern, cleaner) to traverse all directories and filter the files. Here are both options:
Option 1: Using pathlib (Recommended)
from pathlib import Path # Replace with your actual mount point (e.g., /Volumes/WinShare) mounted_share = Path("/Volumes/WinShare") # Recursively find all files ending with .abc.html for html_file in mounted_share.rglob("*.abc.html"): # Print the full absolute path print(f"Found file: {html_file.resolve()}")
Option 2: Using os.walk
import os mounted_share = "/Volumes/WinShare" for root, dirs, files in os.walk(mounted_share): for file in files: if file.endswith(".abc.html"): full_path = os.path.join(root, file) print(f"Found file: {full_path}")
Ubuntu (Production Environment)
Step 1: Mount the Windows Share
First, install the necessary tools to handle SMB shares:
sudo apt update && sudo apt install -y cifs-utils
Then create a mount point and mount the share (replace placeholders with your details):
# Create a mount point (e.g., in /mnt) sudo mkdir -p /mnt/WinShare # Mount the share—uid/gid ensures your user has access to the mounted files sudo mount -t cifs //windows-server-name/share-folder /mnt/WinShare \ -o username=your_username,password=your_password,uid=$(id -u),gid=$(id -g)
For security, you can also store credentials in a file (e.g., ~/.smbcredentials) with restricted permissions instead of passing them in the command:
# ~/.smbcredentials username=your_username password=your_password
Then set permissions:
chmod 600 ~/.smbcredentials
And mount using:
sudo mount -t cifs //windows-server-name/share-folder /mnt/WinShare \ -o credentials=~/.smbcredentials,uid=$(id -u),gid=$(id -g)
To make the mount persistent across reboots, add this line to /etc/fstab:
//windows-server-name/share-folder /mnt/WinShare cifs credentials=/home/your-user/.smbcredentials,uid=1000,gid=1000 0 0
(Replace 1000 with your actual uid/gid—find them with id -u and id -g.)
Step 2: Python Code to Find .abc.html Files
The code is almost identical to Mac—just change the mount point to /mnt/WinShare:
Option 1: Using pathlib
from pathlib import Path mounted_share = Path("/mnt/WinShare") for html_file in mounted_share.rglob("*.abc.html"): print(f"Found file: {html_file.resolve()}")
Option 2: Using os.walk
import os mounted_share = "/mnt/WinShare" for root, dirs, files in os.walk(mounted_share): for file in files: if file.endswith(".abc.html"): full_path = os.path.join(root, file) print(f"Found file: {full_path}")
Key Notes
- Permissions: Make sure your user has read access to the Windows share and the local mount point.
- Unmounting: When done, unmount the share with
umount /Volumes/WinShare(Mac) orsudo umount /mnt/WinShare(Ubuntu). - Special Characters: If your password or share name has special characters, escape them (e.g.,
\$for$) or use the credentials file method on Ubuntu.
内容的提问来源于stack exchange,提问作者Mahesh

