我正在处理一个脚本,在其中输入数字,然后脚本为您创建一个文件夹并在该文件夹中打开文件资源管理器。但是我有问题。
# -*- coding: utf-8 -*-
import subprocess
import os
##saisie du numero de dossier
compostage = str(input('Numero : '))
volume = ('C:')
dossierPrincipal = ('''\\test\\''')
slash = '\\'
##
# Directory
directory = compostage
# Parent Directory path
parent_dir = "C:/test/"
#We make only one var for the func
myPath = parent_dir + directory
# Path
path = os.path.join(myPath)
##We create a condition to be sure te folder is created
if not os.path.exists(path):
os.makedirs(path)
##We inform the user of the creation of the folder
print("Directory '% s' well created" % path)
elif os.path.exists(path):
##We inform the user that the folder in question already exists
print("Directory '% s' already exists" % path)
##We build the entire path
pathComplet = str(volume+dossierPrincipal+compostage)
##Path verification
print(pathComplet)
##Variable Construction
commandeOuverture = str('('+("""r'explorer """)+'"'+myPath+'"'')')
##Directory verification
print ('La commande est : ', commandeOuverture)
##We open the folder using Popen
subprocess.Popen([commandeOuverture], shell=True)
##We open the folder using Popen if the command above doesn't work
#subprocess.Popen(r'explorer "C:\test\"')
输出为:
D:\Users\Alex_computer\Documents\Coding\Python\P4_subprocess>main.py
Numero : 5
Directory 'C:/test/5' already exists
C:\test\5
('La commande est : ', '(r\'explorer "C:/test/5")')
D:\Users\Alex_computer\Documents\Coding\Python\P4_subprocess>The specified path was not found.
这是我启动脚本时所拥有的
我不知道该怎么办,所以才在这个论坛上写
我认为pathlib Path可以使您的生活更加轻松。
从您发布的错误消息中,我了解到您尝试使用正常的路径传递/
...('La commande est : ', '(r\'explorer "C:/test/5")')
但是Windows OS使用反斜杠作为路径分隔符。在下面的代码路径中,首先在变量中打印正确的Windows路径pathComplet
,但是对于您的命令,您使用的myPath显然没有正确的路径定界符。
##Path verification
print(pathComplet)
##Variable Construction
commandeOuverture = str('('+("""r'explorer """)+'"'+myPath+'"'')')
你能试一下吗
from pathlib import Path
myPath = Path(myPath)
PS:很抱歉弄乱了链接https://docs.python.org/3/library/pathlib.html
编辑:
在我的浏览器中打开了正确目录的完整代码:
# -*- coding: utf-8 -*-
import subprocess
from pathlib import Path
##saisie du numero de dossier
directory = str(input('Numero : '))h
parent_dir = Path("C:/test")
myPath = parent_dir / directory
##We create a condition to be sure te folder is created
if not myPath.exists():
myPath.mkdir()
##We inform the user of the creation of the folder
print("Directory '% s' well created" % myPath)
elif myPath.exists():
##We inform the user that the folder in question already exists
print("Directory '% s' already exists" % myPath)
##Variable Construction
commandeOuverture = "explorer " + str(myPath)
##We open the folder using Popen
subprocess.Popen(commandeOuverture, shell=True)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句