如何用python中subprocess.check_output命令将\\172.18.86.7\\test路径映射到网络驱动器T盘
时间: 2023-07-06 13:43:30 浏览: 119
可以使用以下代码将路径映射到 T 盘:
```
import subprocess
subprocess.check_output('net use T: \\\\172.18.86.7\\test', shell=True)
```
该代码使用 `subprocess.check_output` 命令来执行命令行命令 `net use T: \\\\172.18.86.7\\test`,将路径 `\\172.18.86.7\\test` 映射到 T 盘。`shell=True` 参数是指在命令行中执行命令。如果你想在 Python 中使用路径 `T:\`,则可以在命令中指定 `/persistent:yes` 参数:
```
import subprocess
subprocess.check_output('net use T: \\\\172.18.86.7\\test /persistent:yes', shell=True)
```
这将在计算机下一次启动时保留映射。
相关问题
用python的subprocess.check_output写一个脚本,脚本要求:1、将网络路径172.18.86.7\test映射盘符到电脑T,,如果电脑已经有盘符T,则自动使用其他字母做盘符号2、新建盘符时,自动在网络路径上新建一个txt文件(文件内容:电脑名字、IP,时间)
好的,以下是使用`subprocess.check_output`实现的Python脚本:
```python
import subprocess
import os
import socket
import time
# 获取可用的盘符
def get_drive():
drives = ['%s:' % d for d in range(65,91) if os.path.exists('%s:' % chr(d))]
for d in drives:
if not os.path.exists('%s\\' % d):
return d
return None
# 将网络路径映射到电脑
def map_drive(drive_letter, network_path):
subprocess.check_output(['net', 'use', drive_letter + ':', network_path])
# 获取电脑名和IP
def get_computer_info():
computer_name = socket.gethostname()
ip_address = socket.gethostbyname(computer_name)
return (computer_name, ip_address)
# 在网络路径上新建txt文件
def create_txt_file(network_path):
computer_name, ip_address = get_computer_info()
file_name = time.strftime('%Y%m%d%H%M%S') + '.txt'
file_path = os.path.join(network_path, file_name)
with open(file_path, 'w') as f:
f.write('Computer name: %s\nIP address: %s\nTime: %s' % (computer_name, ip_address, time.strftime('%Y-%m-%d %H:%M:%S')))
if __name__ == '__main__':
network_path = r'\\172.18.86.7\test'
drive_letter = get_drive()
if not drive_letter:
print('No available drive letter!')
else:
map_drive(drive_letter, network_path)
create_txt_file(network_path)
```
解释一下脚本的实现过程:
1. `get_drive`函数获取可用的盘符,从'A'盘符一直到'Z'盘符,找到第一个未被使用的盘符。
2. `map_drive`函数将网络路径映射到电脑上,使用`subprocess.check_output`调用命令行工具`net use`实现。
3. `get_computer_info`函数获取电脑名和IP地址,使用Python内置的`socket`库实现。
4. `create_txt_file`函数在网络路径上新建一个txt文件,文件名包含当前时间,文件内容包括电脑名、IP地址和当前时间。
5. 在`__main__`函数中调用以上三个函数,完成脚本的功能。
注意事项:
1. 使用`subprocess.check_output`调用命令行工具时,需要以列表的形式传入命令及其参数。
2. 在使用`open`函数打开文件时,需要指定打开模式为写入模式`'w'`,否则无法写入内容。
3. 在使用`os.path.join`函数拼接文件路径时,需要保证路径中的斜杠方向正确,否则可能会出现路径错误的问题。
python subprocess.check_output
python subprocess.check_output是一个Python标准库subprocess中的函数,可以在Python脚本中执行外部命令并获取命令执行结果。该函数在执行命令时可以指定命令参数,还支持输入和输出的重定向。当命令执行成功且输出结果不为空时,该函数会返回命令输出的字节流。如果命令执行失败或输出为空,则会抛出异常。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20210720083447.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)