import sys
import subprocess
def generateFiles(numFiles):
for i in range(0,numFiles):
postfixDir = str(i)
subprocess.check_call(['mkdir','example' + postfixDir])
return
def main():
numFiles = int(sys.argv[1])
generateFiles(numFiles)
main()
after i enter commandline python .\batchingCommands.py 5
it showing the following erros:
PS E:\Projects\Python_Projects\Python_Automation\Scripts> python .\batchingCommands.py 5
Traceback (most recent call last):
File "E:\Projects\Python_Projects\Python_Automation\Scripts\batchingCommands.py", line 13, in <module>
main()
File "E:\Projects\Python_Projects\Python_Automation\Scripts\batchingCommands.py", line 11, in main
generateFiles(numFiles)
File "E:\Projects\Python_Projects\Python_Automation\Scripts\batchingCommands.py", line 6, in generateFiles
subprocess.check_call(['mkdir','example' + postfixDir])
File "E:\Development_tools\Python39\lib\subprocess.py", line 368, in check_call
retcode = call(*popenargs, **kwargs)
File "E:\Development_tools\Python39\lib\subprocess.py", line 349, in call
with Popen(*popenargs, **kwargs) as p:
File "E:\Development_tools\Python39\lib\subprocess.py", line 951, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "E:\Development_tools\Python39\lib\subprocess.py", line 1420, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] The system cannot find the file specified
i'm trying to create directory automatically
Python is telling you it asked the operating system to run the program mkdir, which doesn't exist. Unlike in unix, mkdir isn't an actual program on Windows - it's built into powershell and command.com themselves.
To instruct python to run the given command as arguments to a shell - instead of a raw program - use the shell kwarg.
>>> subprocess.check_call(['mkdir', 'foo'], shell=True)
0
Related
I am trying to get a tree of function-API under FreeCAD which is defined as App inside freecad always.
My goal is to find in the API where the OpenGL context is used and how it is used so I can inject my own OpenGL code/other libraries.
Here is the code
import FreeCAD as App
import FreeCADGui as Gui
from anytree import Node, RenderTree
from anytree.exporter import DotExporter
from inspect import getmembers, isfunction
from pkgutil import iter_modules
directory="E:/TEMP/DOC/"
MainNoid=Node("FREECAD")
def retriveOneObj(_objName,nodename):
mainNode=nodename
_OldNode=nodename
try:
for objS in getmembers(_objName):
Gui.updateGui()
for tobj in getmembers(objS):
if type(tobj) is tuple:
nObj=Node(str(tobj),_OldNode)
else:
nObj=Node(tobj.__name__,_OldNode)
return mainNode
except Exception as err:
print (err)
def retriveAll():
freecad=Node(App.__name__)
allNode=retriveOneObj(App,freecad)
DotExporter(allNode).to_picture(directory+"freecad.png")
retriveAll()
I get the error below :
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "<input>", line 4, in retriveAll
File "D:\Users\...\Downloads\FreeCAD\bin\lib\site-packages\anytree\exporter\dotexporter.py", line 272, in to_picture
check_call(cmd)
File "D:\Users\...\Downloads\FreeCAD\bin\lib\subprocess.py", line 359, in check_call
retcode = call(*popenargs, **kwargs)
File "D:\Users\...\Downloads\FreeCAD\bin\lib\subprocess.py", line 340, in call
with Popen(*popenargs, **kwargs) as p:
File "D:\Users\...\Downloads\FreeCAD\bin\lib\subprocess.py", line 858, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "D:\Users\...\Downloads\FreeCAD\bin\lib\subprocess.py", line 1311, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] The file can not be found
This is on Windows10
Note: You need to run this inside FreeCAD and install the libraries using pip found under the \FreeCAD\bin\Scripts folder
I am trying to automate my build process. typically what I do is I go to a certain folder where my make scripts are at, open a CMD, type a command which is something like this "Bootstrap_make.bat /fic = Foo"
Now I want to do this from inside my python code. simple question is How to do this, i.e. how to; using python code. go to a certain path > and open a CMD > and execute the command"
here is what I've tried
def myClick():
subprocess.run(["cd ..\Projects\ADAS\proj11\embedded_env\make_scripts" ])
subprocess.run(["bootstrap_make.bat","/fic= my_software_variant"])
However I git this error :
Traceback (most recent call last):
File "C:\Users\aelkhate\AppData\Local\Programs\Python\Python310\lib\tkinter_init_.py", line 1921, in call
return self.func(*args)
File "C:\E\ProductOne_Builder1.py", line 5, in myClick
subprocess.run(["cd ..\Projects\ADAS\proj11\embedded_env\make_scripts" ])
File "C:\Users\aelkhate\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 501, in run
with Popen(*popenargs, **kwargs) as process:
File "C:\Users\aelkhate\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 966, in init
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:\Users\aelkhate\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 1435, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] The system cannot find the file specified
You could use os.system().
https://docs.python.org/3/library/os.html
This code is assuming you are on Windows because you are running a .bat file and trying to open a CMD window.
import os
directory = './path/to/make/scripts'
buildCommand = './Bootstrap_make.bat'
buildArgs = '/fic = Foo'
# if you don't want a new cmd window you could do something like:
os.system('cd {} && {} {}'.format(directory, buildCommand, buildArgs))
# if you want a new cmd window you could do something like:
os.system('cd {} && start "" "{}" {}'.format(directory, buildCommand, buildArgs))
You can use subprocess.run, specifying the cwd (current working directory) kwarg.
I wanted to create a Minecraft Launcher using Python today, but the game cannot be started.
My Code:
import minecraft_launcher_lib
import subprocess
minecraft_directory = "C:\\Users\\Mert KAPLANDAR\\AppData\\Roaming\\.minecraft"
options = minecraft_launcher_lib.utils.generate_test_options()
minecraft_command = minecraft_launcher_lib.command.get_minecraft_command("1.8.9", minecraft_directory, options)
subprocess.call(minecraft_command)
Error:
Traceback (most recent call last):
File "C:\Users\Mert KAPLANDAR\Desktop\launcher.py", line 10, in <module>
subprocess.call(str(minecraft_command))
File "C:\Users\Mert KAPLANDAR\AppData\Local\Programs\Python\Python39\lib\subprocess.py", line 349, in call
with Popen(*popenargs, **kwargs) as p:
File "C:\Users\Mert KAPLANDAR\AppData\Local\Programs\Python\Python39\lib\subprocess.py", line 951, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:\Users\Mert KAPLANDAR\AppData\Local\Programs\Python\Python39\lib\subprocess.py", line 1420, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] Sistem belirtilen dosyayı bulamıyor
#Mert Kaplander, I would believe the error might be because you don't have that version of Minecraft installed. You can check with the following method:
minecraft_launcher_lib.utils.get_installed_versions(minecraft_dir)
example:
import subprocess
import minecraft_launcher_lib
from colorama import Fore, init
for version in minecraft_launcher_lib.utils.get_installed_versions(minecraft_dir):
print(Fore.GREEN+ f"{version}")
Other than that, your code is an exact copy from the documentation. So I don't see an other error.
I'm trying to delete the last line of the command line by calling os.system("cls") but that returns a weird symbol
I tried using sub-processing to delete the last line but doing that returns an Error
Traceback (most recent call last):
File "D:\GameDev-Programming\Python\puzzles\puzzles 1\main.py", line 11, in <module>
clear()
File "D:\GameDev-Programming\Python\puzzles\puzzles 1\main.py", line 6, in clear
x = call('clear' if os.name == 'posix' else 'cls')
File "C:\Users\USER\AppData\Local\Programs\Python\Python39\lib\subprocess.py", line 349, in call
with Popen(*popenargs, **kwargs) as p:
File "C:\Users\USER\AppData\Local\Programs\Python\Python39\lib\subprocess.py", line 951, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:\Users\USER\AppData\Local\Programs\Python\Python39\lib\subprocess.py", line 1420, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] The system cannot find the file specified
The code that i copied from the internet
from subprocess import call
import os
def clear():
x = call('clear' if os.name == 'posix' else 'cls')
print("fa")
clear()
How do I fix these Issues? if I can't is there other ways to delete the last line of the Command-Line?
Edit:
Looks like the issue is from Pycharm(the ide im using)
I turned on the 'Emulate terminal in output console' in 'Run/Debug Configuration'
The error message ...
FileNotFoundError: [WinError 2] The system cannot find the file specified
... implies that the command you are trying to execute is not an external command. So it must be a command that is implemented by the shell. You need to add the shell=True argument:
def clear():
x = call('clear' if os.name == 'posix' else 'cls', shell=True)
I am using PyCharm to run simple pyflink latest 1.13 example (it is only two lines). But the get_gateway() method is not working.
For the interpreter I created virtual environment and use it in my PyCharm project. I also downloaded Java8.
I tried many steps to solve the issue but
from pyflink.common.serialization import Encoder
from pyflink.common.typeinfo import Types
from pyflink.datastream import StreamExecutionEnvironment
from pyflink.datastream.connectors import StreamingFileSink
def print_hi(name):
# Use a breakpoint in the code line below to debug your script.
print(f'Hi, {name}') # Press Ctrl+F8 to toggle the breakpoint.
env = StreamExecutionEnvironment.get_execution_environment()
env.set_parallelism(1)
C:\Users\myuser\AppData\Local\Programs\Python\Python38\myvenv\Scripts\python.exe C:/Users/myuser/PycharmProjects/pythonProject3/main.py
Hi, PyCharm
Traceback (most recent call last):
File "C:/Users/myuser/PycharmProjects/pythonProject3/main.py", line 19, in <module>
print_hi('PyCharm')
File "C:/Users/myuser/PycharmProjects/pythonProject3/main.py", line 13, in print_hi
env = StreamExecutionEnvironment.get_execution_environment()
File "C:\Users\myuser\AppData\Local\Programs\Python\Python38\myvenv\lib\site-packages\pyflink\datastream\stream_execution_environment.py", line 688, in get_execution_environment
gateway = get_gateway()
File "C:\Users\myuser\AppData\Local\Programs\Python\Python38\myvenv\lib\site-packages\pyflink\java_gateway.py", line 62, in get_gateway
_gateway = launch_gateway()
File "C:\Users\myuser\AppData\Local\Programs\Python\Python38\myvenv\lib\site-packages\pyflink\java_gateway.py", line 106, in launch_gateway
p = launch_gateway_server_process(env, args)
File "C:\Users\myuser\AppData\Local\Programs\Python\Python38\myvenv\lib\site-packages\pyflink\pyflink_gateway_server.py", line 221, in launch_gateway_server_process
return Popen(command, stdin=PIPE, preexec_fn=preexec_fn, env=env)
File "C:\Users\myuser\AppData\Local\Programs\Python\Python38\lib\subprocess.py", line 858, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:\Users\myuser\AppData\Local\Programs\Python\Python38\lib\subprocess.py", line 1311, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] The system cannot find the file specified
See similar question here for description: https://stackoverflow.com/a/71840491/272023
JAVA_HOME was not pointing to a valid Java installation.