How to clear all output in Vte.Terminal ?
use the os library
import os
os.system('clear')
It would only clear the text visible in the terminal, but, you can run the cls command or tinker with the feed() method and empty spaces.
I have been having some problems of my own with Vte so I don't know if I'm the right person to answer.
Have you tried replacing the old terminal with another in the container? It's not clearing, but there will be an empty terminal.
vte.feed('\033[2J')
For other escape sequences:
Bash Prompt HOWTO
ANSI escape code (Wikipedia)
I'm using
vte.fork_command("clear")
Related
Let's say I have a network path like this below:
\\srv\teams\dir 1
How can I open it using subprocess? I am trying:
subprocess.Popen("explorer '\\srv\teams\dir 1'")
but it always leads me to my 'My Documents'. It works fine from cmd. I am using win7.
I also tried:
os.system("explorer '\\srv\teams\dir 1'")
Please see Mike Scotty for solution regarding os.system. If you use subprocess, please use a list of string for your command instead of a single string:
subprocess.call(['explorer', '\\\\srv\\teams\\dir 1'])
Note that I use subprocess.call instead of subprocess.Popen since this is a simple call, no need to overkill
There are two issues with your code:
1) Use a raw string or escape your \ characters
2) Use " instead of ' to enclose the path
os.system(r'explorer "\\srv\teams\dir 1"')
I made this code and it is working but only in Linux.
import subprocess as sub
sub.Popen([r"Rscript","diccionari.R"])
Where "diccionari.R" is the name of my script in R.
Error text message: System can't found the specific file.
Can somebody help me and do that it works on windows please?
Thank you.
You should probably try the slashes the other way around as how I said it earlier.
Using full path to the .r script (e.g. "C:/myfolder/diccionari.R") instead of just the script file, and using OS independent slashes.
You should specify where Rscriptis located i.e
import subprocess as sub
cmd_line = [r"C:\\Program Files\\R\\R-3.6.0\\bin\\Rscript", "diccionari.R"]
sub.Popen(cmd_line)
watch for the \\ characters
I know all about how Windows uses backslashes for filenames, etc., and Unix uses forward. However, I never use backslashes with strings I create in my code. However:
When windows explorer "drops" a file onto a python script, the string it passes contains backslashes. These translate into escape sequences in the strings in the sys.argv list and then I have no way to change them after that (open to suggestions there)
Is there any way I can somehow make windows pass a literal string or ... any other way I can solve this problem?
I'd love my script to be droppable, but the only thing preventing me is windows backslashes.
EDIT:
Sorry everyone, the error was actually not the passing of the string - as someone has pointed out below, but this could still help someone else:
Make sure you use absolute path names because when the Windows shell will NOT run the script in the current directory as you would from a command line. This causes permission denied errors when attempting to write to single-part path-names that aren't absolute.
Cannot reproduce. This:
import os, sys
print sys.argv
print map(os.path.exists, sys.argv)
raw_input()
gives me this:
['D:\\workspaces\\generic\\SO_Python\\9266551.py', 'D:\\workspaces\\generic\\SO_Python\\9254991.py']
[True, True]
after dropping the second file onto the first one. Python 2.7.2 (on Windows). Can you try this code out?
I'm having trouble in my python script, and I don't understand it :
subprocess.call(['convert', file, '-crop', '80x10+90+980', '+repage', 'test.jpg'])
Returns "invalid argument - -crop"
But if I run this from the command line, it works fine :
convert test.jpg -crop 80x10+90+980 +repage test.jpg
What am I missing here ?
Is there more than one convert in the system? Try an absolute path to the command you want?
What about using the python image library instead? That seems much more reliable than to call a subprocess (especially for error handling...).
file is a _____builtin_____ class. Overriding it may produce unwanted results. Try using a different variable name.
I've actually tried your code:
>>> import subprocess
>>> subprocess.call(['convert', 'capa.jpg', '-crop', '80x10+90+980', '+repage', 'capa2.jpg'])
0
>>>
And it works for me!
So you must have something wrong, somewhere else. Check our assumptions again.
I have a simple python script like so:
import sys
lines = sys.argv[1]
for line in lines.splitlines():
print line
I want to call it from the command line (or a .bat file) but the first argument may (and probably will) be a string with multiple lines in it. How does one do this?
Of course, this works:
import sys
lines = """This is a string
It has multiple lines
there are three total"""
for line in lines.splitlines():
print line
But I need to be able to process an argument line-by-line.
EDIT: This is probably more of a Windows command-line problem than a Python problem.
EDIT 2: Thanks for all of the good suggestions. It doesn't look like it's possible. I can't use another shell because I'm actually trying to invoke the script from another program which seems to use the Windows command-line behind the scenes.
I know this thread is pretty old, but I came across it while trying to solve a similar problem, and others might as well, so let me show you how I solved it.
This works at least on Windows XP Pro, with Zack's code in a file called
"C:\Scratch\test.py":
C:\Scratch>test.py "This is a string"^
More?
More? "It has multiple lines"^
More?
More? "There are three total"
This is a string
It has multiple lines
There are three total
C:\Scratch>
This is a little more readable than Romulo's solution above.
Just enclose the argument in quotes:
$ python args.py "This is a string
> It has multiple lines
> there are three total"
This is a string
It has multiple lines
there are three total
The following might work:
C:\> python something.py "This is a string^
More?
More? It has multiple lines^
More?
More? There are three total"
This is the only thing which worked for me:
C:\> python a.py This" "is" "a" "string^
More?
More? It" "has" "multiple" "lines^
More?
More? There" "are" "three" "total
For me Johannes' solution invokes the python interpreter at the end of the first line, so I don't have the chance to pass additional lines.
But you said you are calling the python script from another process, not from the command line. Then why don't you use dbr' solution? This worked for me as a Ruby script:
puts `python a.py "This is a string\nIt has multiple lines\nThere are three total"`
And in what language are you writing the program which calls the python script? The issue you have is with argument passing, not with the windows shell, not with Python...
Finally, as mattkemp said, I also suggest you use the standard input to read your multi-line argument, avoiding command line magic.
Not sure about the Windows command-line, but would the following work?
> python myscript.py "This is a string\nIt has multiple lines\there are three total"
..or..
> python myscript.py "This is a string\
It has [...]\
there are [...]"
If not, I would suggest installing Cygwin and using a sane shell!
Have you tried setting you multiline text as a variable and then passing the expansion of that into your script. For example:
set Text="This is a string
It has multiple lines
there are three total"
python args.py %Text%
Alternatively, instead of reading an argument you could read from standard in.
import sys
for line in iter(sys.stdin.readline, ''):
print line
On Linux you would pipe the multiline text to the standard input of args.py.
$ <command-that-produces-text> | python args.py