Skip to content

Instantly share code, notes, and snippets.

@selfboot
Last active November 25, 2021 14:10
Show Gist options
  • Save selfboot/5609821 to your computer and use it in GitHub Desktop.
Save selfboot/5609821 to your computer and use it in GitHub Desktop.
mac os x:自动挂载ntfs硬盘为读写权限。 只要ntfs硬盘连接到电脑即可使用 ./ntfs_mount_auto.py 挂载ntfs磁盘为可读写,ntfs_unmount.py 为卸载磁盘。 ntfs_mount.py 是较早的版本,只有电脑先识别除硬盘,在/Volumes 可读到硬盘内容时才可以使用此脚本挂载为可读写。 建议使用./ntfs_mount_auto.py
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import subprocess
import re
ntfs_pattern = re.compile(r'File System Personality: NTFS')
ntfs_device_node = re.compile(r'.*Device Node:.*')
device_dict = {}
def get_device_node():
disk = subprocess.check_output(['ls', '-1', '/Volumes'])
disk_list = disk.split("\n")
for disk_name in disk_list[:-1]:
disk_path = '/Volumes/' + disk_name
try:
disk_info = subprocess.check_output(['diskutil',
'info',
disk_path])
except subprocess.CalledProcessError, e:
print "diskutil stderr output:\n", e.output
if ntfs_pattern.search(disk_info):
device_node_str = ntfs_device_node.findall(disk_info)
device_node = device_node_str[0].split()[2]
device_dict[disk_path] = device_node
def mount_ntfs():
if not device_dict:
print "No ntfs filesystem found..."
return
print "hdiutil detach disk..."
subprocess.check_output(['hdiutil', 'detach', device_dict.keys()[0]])
for disk_path in device_dict.keys():
device_node = device_dict[disk_path]
print "mkdir %s" % disk_path
subprocess.check_output(['mkdir', disk_path])
print "sudo mount_ntfs -o rw,nobrowse %s %s" % (device_node, disk_path)
subprocess.check_output(['sudo',
'mount_ntfs',
'-o',
'rw,nobrowse',
device_node, disk_path])
if __name__ == '__main__':
get_device_node()
mount_ntfs()
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import subprocess
import re
import os
ntfs_pattern = re.compile(r'.*NTFS.*')
device_dict = {}
def get_device_node():
disk = subprocess.check_output(['diskutil', 'list'])
disk_list = disk.split("\n")
for info in disk_list:
if ntfs_pattern.search(info):
info_list = info.split()
disk_path = "/Volumes/%s" % info_list[2]
device_node = "/dev/%s" % info_list[5]
device_dict[disk_path] = device_node
def mount_ntfs():
if not device_dict:
print "No ntfs filesystem found..."
return
for disk_path in device_dict.keys():
device_node = device_dict[disk_path]
if os.path.isdir(disk_path):
subprocess.check_output(['hdiutil', 'detach', disk_path])
print "mkdir %s" % disk_path
subprocess.check_output(['mkdir', disk_path])
print "sudo mount_ntfs -o rw,nobrowse %s %s" % (device_node, disk_path)
subprocess.check_output(['sudo',
'mount_ntfs',
'-o',
'rw,nobrowse',
device_node, disk_path])
if __name__ == '__main__':
get_device_node()
mount_ntfs()
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import subprocess
import re
ntfs_pattern = re.compile(r'File System Personality: NTFS')
ntfs_device_node = re.compile(r'.*Device Node:.*')
device_dict = {}
def get_device_node():
disk = subprocess.check_output(['ls', '-1', '/Volumes'])
disk_list = disk.split("\n")
for disk_name in disk_list[:-1]:
disk_path = '/Volumes/' + disk_name
try:
disk_info = subprocess.check_output(['diskutil',
'info',
disk_path])
except subprocess.CalledProcessError, e:
print "diskutil stderr output:\n", e.output
if ntfs_pattern.search(disk_info):
device_node_str = ntfs_device_node.findall(disk_info)
device_node = device_node_str[0].split()[2]
device_dict[disk_path] = device_node
def mount_ntfs():
if not device_dict:
print "No ntfs filesystem found..."
return
detach_info = subprocess.check_output(['hdiutil',
'detach',
device_dict.keys()[0]])
print detach_info
if __name__ == '__main__':
get_device_node()
mount_ntfs()
@aiofmliuyu
Copy link

您好,使用*_auto.py以后,ntfs只有读权限,没有写权限,还是需要修改/etc/fstab文件才可以,能否加入写fstab的方法?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment