如何在 python 脚本中使用 "code ." 命令

how to use "code ." command in python script

我编写了一段代码来自动创建一个新项目。我已经设法在我喜欢的位置创建文件,创建文件并创建测试 python 文件。我如何在 vs 代码中打开该文件?

import subprocess,os,time

projectName = input("What is the name of the project?\n")
filename = "test"
fileEx = '.py'

os.chdir('../')
os.chdir('../')
os.chdir('../')
os.chdir('.\Documents\ ')
os.chdir('.\programs\ ')
project = subprocess.Popen(["powershell","md",projectName])
file = filename + fileEx
fileLoctaion = os.getcwd() + file
d = os.getcwd() + f'\{projectName}\ '
time.sleep(1)
os.chdir(d)
with open(file, 'w') as fp:
    pass

您可以尝试以下方法:

import os
os.system("code NAMEOFFILE.py") ## the "code" command is the command used to open a file with vsc in a command line interface.

你可以用子进程做同样的事情:

import subprocess
subprocess.getoutput("code NAMEOFFILE.py")