You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Mac及Ubuntu环境下Python连接Windows共享路径并检索指定文件

How to Access Windows Shares & Find .abc.html Files via Python on Mac/Ubuntu

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_smbfs to mount the share to a local mount point (create the mount point first if it doesn't exist):
    # 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
    
    If you don't want to put your password in plaintext, omit it from the command—you'll be prompted to enter it securely.
  • Finder method: You already know this, but just to confirm: Go to Go > Connect to Server, enter smb://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) or sudo 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 08:28:14