I need to quote a path in a portable way, shlex.quote is converting slashes to / on windows, which causes the command to fail (using the default windows command prompt).
Returning C:/Users/Me/Documents/Projects/Test
Instead of C:\Users\Me\Documents\Projects\Test
How can I quote a path on windows, similar to shlex.quote that uses native slashes, suitable for the windows command prompt and BAT files?
As mentioned by some comments, your problem might not be caused by shlex.quote().
Nevertheless there is the module mslex which provides mslex.quote() and mslex.split() for the Windows shell, just like what shlex provides for the Unix shell.
Related
What is the difference between the ./file.py & the python file.py commands?
What I See
I usually see people using ./file.py when they are using a terminal-text editor like vim or nano or emacs OR when they are using linux based operating systems like Ubuntu or Arch Linux.
And I usually see python file.py from the ones who are using some other operating system. I’m probably not correct. But if it is so, what is the difference between the both?
Thank You!
on linux-based operating systems, when you execute a text file, if it starts with #!/bin/python (shebang syntax), it will actually do /bin/python filename, so it is faster to do this than having to type python all the time, it is easier to make it an executable file, but there are no major differences
In Linux and Unix operating systems, you can execute an executable (./file.py) without explicitly specifying it as a python file, while you can as well execute same file.py as python file.py in both Windows and non-Windows Operating system.
For this python file to be executed without explicitly prefixing python to it, it must surely meet these two requirements:
Must contain a shebang line:
A sha-bang/shebang line is a line of code which consists of characters that tells the loader to use the specified interpreter, just as stated in the docs
If the first line of a script file starts with #!, it is known as a
“shebang” line. Linux and other Unix like operating systems have
native support for such lines and they are commonly used on such
systems to indicate how a script should be executed. This launcher
allows the same facilities to be used with Python scripts on Windows
and the examples above demonstrate their use.
To allow shebang lines in Python scripts to be portable between Unix
and Windows, this launcher supports a number of ‘virtual’ commands to
specify which interpreter to use. The supported virtual commands are:
/usr/bin/env python
/usr/bin/python
/usr/local/bin/python
python
Must be an executable file
In Linux and Unix OS, files are treated separately depending on their permissions and mode, you must make file.py an executable before calling it without explictly prefixing python/python3, to make file.py executable, you need to change the mode to an executable by running chmod +x file.py from the file directory in Linux terminal.
According to https://jcastellssala.com/2012/07/20/python-command-line-waiting-feedback-and-some-background-on-why/, '\r\x1b[K' is an escaping sequence that erases the current line in the console and rewrites something in Python. But when I tried to use the sequence on Windows cmd, it prints out weird characters instead. In Python, is there an equivalent sequence/action on Windows cmd where I can erase the last line I print out to the console?
Amazingly, support for ANSI escape sequences in the Windows console was only added in Windows 10 Version 1511:
http://www.nivot.org/blog/post/2016/02/04/Windows-10-TH2-(v1511)-Console-Host-Enhancements
They will not work in older versions of Windows, unless you use a terminal emulator which supports them, like ConEmu:
https://conemu.github.io/
I know you can run Linux terminal commands through Python scripts using subprocess
subprocess.call(['ls', '-l']) # for linux
But I can't find a way to do the same thing on windows
subprocess.call(['dir']) # for windows
is it possible using Python without heavy tinkering?
Should I stick to good old fashioned batch files?
dir is not a file, it is an internal command, so the shell keyword must be set to True.
subprocess.call(["dir"], shell=True)
Try this
import os
os.system("windows command")
ex: for date
os.system("date")
Almost everyone's answers are right but it seems I can do what I need using os.popen -- varStr = os.popen('dir /b *.py').read()
First of all, to get a directory listing, you should rather use os.listdir(). If you invoke dir instead, you'll have to parse its output to make any use of it, which is lots of unnecessary work and is error-prone.
Now,
dir is a cmd.exe built-in command, it's not a standalone executable. cmd.exe itself is the executable that implements it.
So, you have two options (use check_output instead of check_call if you need to get the output instead of just printing it):
use cmd's /C switch (execute a command and quit):
subprocess.check_call(['cmd','/c','dir','/s'])
use shell=True Popen() option (execute command line through the system shell):
subprocess.check_call('dir /s', shell=True)
The first way is the recommended one. That's because:
In the 2nd case, cmd, will do any shell transformations that it normally would (e.g. splitting the line into arguments, unquoting, environment variable expansion etc). So, your arguments may suddenly become something else and potentially harmful. In particular, if they happen to contain any spaces and cmd special characters and/or keywords.
shell=True uses the "default system shell" (pointed to via COMSPEC environment variable in the case of Windows), so if the user has redefined it, your program will behave unexpectedly.
I'm doing some work with a custom version of Python 2.6 on Windows, and would love to use emacs for this purpose. On more unixy platforms, I've never had any issue with the basic python-mode stuff, but now I'm encountering a problem when I try to use the M-x run-python command.
My custom Python is on the path (i.e. can type python from a windows command prompt and get the appropriate version). Unfortunately, I simply get the following error in my *Messages* buffer when attempting to start it with run-python from emacs:
apply: Spawning child process: invalid argument
Is anyone running a similar python config that can shed light on this? Happy to post any other config details as needed.
On Windows, when we use standard backward slashes in the init.el file (.emacs):
(setq python-shell-interpreter "c:\myRoot\Proframs\Python33\python3.exe")
Emacs interprets this as:
c:myRootProframsPython33python3.exe
This what is the invalid argument I think.
Thus we can:
(setq python-shell-interpreter "c:/myRoot/Proframs/Python33/python3.exe")
Now C-u M-x "run-python" works.
You could try C-u M-x run-python, which gives you the option of selecting the path of the python binary you want to run.
Alternatively, if you would like to start it using Elisp, that can be achieved by calling (run-python "/usr/bin/python3") for python3, for example.
I am writing a Python script on Windows, that needs to work on a Red Hat Linux machine. On Windows,
os.path.abspath(os.curdir)
returns something like
C:\Users\Me\...\CurrentDirectory
without a trailing \ .
I'm unable to run it on Linux, but to my knowledge it would be more like
/home/Me/.../CurrentDirectory
with the slashes going the other way, and I'm uncertain about a trailing / . First of all, is there one? Secondly, how do I deal with this issue? The script doesn't have to work on both Windows and Linux, just Linux, in the end. Though I can only test it on Windows :(
It shouldn't matter if you use os.path.join()
See:
Python os.path.join on Windows
Why doesn't os.path.join() work in this case?
The behavior would be more or less the same across the OS. Instead of using \ or / use
os.sep
inside your code. Do not try to hardcode anything