我试图将这个小的 .bat 脚本重写成一个 .py 文件,主要是为了练习,但我在让整个 shutil 模块以我的方式工作时遇到了很多麻烦似乎有很多方法可以去做使用复制、删除和目录创建。
但我的主要希望是有人可以将这些基本命令转换成一个工作脚本,这样我在处理其他脚本时就可以有所作为,并希望最终我会通过使用开始理解。
任何帮助都会很棒! :)
SET USERNAME
:: Create new directory
md "C:\Users\%USERNAME%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Data"
COPY "Data" "C:\Users\%USERNAME%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Data"
COPY "System32.lnk" "C:\Users\%USERNAME%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup"
start /d "C:\Users\%USERNAME%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Data" explorer.exe
start /d "C:\Users\%USERNAME%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Data" PnkBstrA.exe
启动问题:
嘿,很抱歉重新发布,但脚本无法在脚本末尾启动两个应用程序,如果我只将启动命令从脚本中取出并将它们放入如下所示的新脚本中。它会启动第一个应用程序,并在第一个应用程序关闭后启动下一个应用程序。
它似乎也被设置为一个接一个地启动应用程序,导致它们不能同时运行。
import os
import shutil
import subprocess
data_dir = os.path.expandvars(r"C:\Users\%USERNAME%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Data")
menu_dir = os.path.expandvars(r"C:\Users\%USERNAME%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup")
subprocess.call(['explorer.exe', data_dir])
subprocess.call(['PnkBstrA.exe'], cwd=data_dir)
编辑:我还需要将这个编辑过的启动文件直接放在文件夹中,其中包含它应该启动的 2 个程序才能正常运行。
打开测试:
import os
import shutil
import subprocess
data_dir = os.path.expandvars(r"C:\Users\%USERNAME%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Data")
menu_dir = os.path.expandvars(r"C:\Users\%USERNAME%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup")
subprocess.Popen(['explorer.exe', data_dir])
subprocess.call(['PnkBstrA.exe'], cwd=data_dir)
编辑的路径名:
import os
import shutil
import subprocess
data_dir = os.path.expandvars(r"C:\Users\%USERNAME%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Data")
menu_dir = os.path.expandvars(r"C:\Users\%USERNAME%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup")
subprocess.Popen(['explorer.exe', data_dir])
subprocess.call([r'C:\C:\Users\Spanjer\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Data\Data\PnkBstrA.exe'], cwd=data_dir)
编辑的 PathName CMD 读数:
C:\Users\Spanjer\Desktop>installtest2.py
Traceback (most recent call last):
File "C:\Users\Spanjer\Desktop\installtest2.py", line 9, in <module>
subprocess.call([r'C:\C:\Users\Spanjer\AppData\Roaming\Microsoft\Windows\Sta
rt Menu\Programs\Data\Data\PnkBstrA.exe'], cwd=data_dir)
File "C:\Python27\lib\subprocess.py", line 524, in call
return Popen(*popenargs, **kwargs).wait()
File "C:\Python27\lib\subprocess.py", line 711, in __init__
errread, errwrite)
File "C:\Python27\lib\subprocess.py", line 948, in _execute_child
startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
C:\Users\Spanjer\Desktop>
最终脚本 100% 工作:
import os
import shutil
import subprocess
data_dir = os.path.expandvars(r"C:\Users\%USERNAME%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Data")
menu_dir = os.path.expandvars(r"C:\Users\%USERNAME%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup")
dest_dir = os.path.join(data_dir, r'Data')
if not os.path.exists(data_dir):
os.mkdir(data_dir)
if os.path.exists(dest_dir):
shutil.rmtree(dest_dir)
shutil.copytree(r"Data", dest_dir)
shutil.copy(r"System32.lnk", menu_dir)
subprocess.Popen([r'C:\Users\Spanjer\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Data\Data\explorer.exe'], cwd=data_dir)
subprocess.call([r'C:\Users\Spanjer\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Data\Data\PnkBstrA.exe'], cwd=data_dir)
请您参考如下方法:
试试下面的代码:
import os
import shutil
import subprocess
data_dir = os.path.expandvars(r"C:\Users\%USERNAME%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Data")
menu_dir = os.path.expandvars(r"C:\Users\%USERNAME%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup")
dest_dir = os.path.join(data_dir, r'Data')
if not os.path.exists(data_dir):
os.mkdir(data_dir)
if os.path.exists(dest_dir):
shutil.rmtree(dest_dir)
shutil.copytree(r"Data", dest_dir)
shutil.copy(r"System32.lnk", menu_dir)
subprocess.call(['explorer.exe', data_dir])
subprocess.call(['PnkBstrA.exe'], cwd=data_dir)
os.path.expandvars(path)
Return the argument with environment variables expanded. Substrings of the form $name or ${name} are replaced by the value of environment variable name. Malformed variable names and references to non-existing variables are left unchanged.
On Windows, %name% expansions are supported in addition to $name and ${name}.
os.mkdir(path[, mode])
Create a directory named path with numeric mode mode. The default mode is 0777 (octal). On some systems, mode is ignored. Where it is used, the current umask value is first masked out. If the directory already exists, OSError is raised.
shutil.copy(src, dst)
Copy the file src to the file or directory dst. If dst is a directory, a file with the same basename as src is created (or overwritten) in the directory specified. Permission bits are copied. src and dst are path names given as strings.
*subprocess.call(args, , stdin=None, stdout=None, stderr=None, shell=False)
Run the command described by args. Wait for command to complete, then return the returncode attribute.
The arguments shown above are merely the most common ones, described below in Frequently Used Arguments (hence the slightly odd notation in the abbreviated signature). The full function signature is the same as that of the Popen constructor - this functions passes all supplied arguments directly through to that interface.
