I have a problem with a script I wrote.
I'm just trying to run an executable (I took arduino.exe as an example). However, I either get a FileNotFoundError: [WinError 2] or a non-zero exit status (depending on Shell=True is off or on, respectively).
The code of my entire script is simple:
import subprocess
subprocess.run("C:\Program Files (x86)\Arduino\arduino.exe",shell=True,check=True)
I am aware that Shell=True poses a security risk, but have found no other way to solve the path not found error. My other guess is that the code struggles with the numbers and spaces in the path?
Any help is greatly appreciated.
you need to escape backslash character.
use \\ instead of \ for every backslash,
subprocess.run("C:\\Program Files (x86)\\Arduino\\arduino.exe",shell=True,check=True)
or you can use raw string literal,
subprocess.run(r"C:\Program Files (x86)\Arduino\arduino.exe",shell=True,check=True)
Related
I'm trying to run a python script using Sublime Text 3. I'm trying to just use Ctrl+b and type the parameters in the box that comes up, but I can't for the life of me figure out how to format the Windows file paths. I keep getting a FileNotFoundError
I've tried:
"C:\dir1\dir 2\file.ext"
"C:\\dir1\\dir 2\\file.ext"
"C:/dir1/dir 2/file.ext"
Because some of my directories have spaces in them, I'm enclosing the whole thing in double quotes no matter which slash style I try. What am I missing here? None of these works.
With the first, for example, I'm getting FileNotFoundError: [Errno 2] No such file or directory: 'C:\\dir1\\dir 2\\file.ext,' [Finished in 0.3s]
The file is most definitely there and spelled correctly.
In case it matters, I'm using docopt to parse the input parameters
I'm trying to read in a text file to work with Word Clouds. Here is the syntax I'm trying:
# Read the whole text.
text = open(r'C:\Users\mswitajski\Desktop\alice.txt').read()
But I keep getting the following error:
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\mswitajski\\Desktop\\alice.txt'
I've triple checked the file name, tried reading it as a raw file, changed the slashes and everything but I continue to get the same error.
Well, if someone reaches up to here and still could not find the solution then here is the more pythonic way of doing the absolute path in windows.
Instead of using:
text = open(r'C:\Users\mswitajski\Desktop\alice.txt').read()
use os.sep, in conjunction of os.path.join like the following:
import os
text = open(os.path.join('C:', os.sep, 'Users', 'mswitajski', 'Desktop', 'alice.txt')).read()
Try changin the path to this"
'C:\Users\mswitajski\Desktop/alice.txt'
Sometimes windows won't find/recognize the file path when the file is specified like this
'C:\Users\mswitajski\Desktop\alice.txt'
In the answer it shows up as only one \ but you still need 2 like your previous path. The only difference is the last slash /. Hope that works.
At your text raw file (alice.txt) try delete the .txt.
The file probably is named alice.txt.txt
I face the same issue and solve it by deleted the .txt.
I had to use double slashes instead of one, because python interpreted it as a escape sequence. My final string was:
C:\\Users\\ArpitChinmay's\\AppData\\Roaming\\Code\\User\\globalStorage\\moocfi.test-my-
code\\tmcdata\\TMC workspace\\Exercises\\hy\\hy-data-analysis-with-python-
2020\\part02-e04_word_frequencies\\src\\alice.txt
However, it worked this way too,
C:\\Users\\Arpit Chinmay's\\AppData\\Roaming\\Code\\User\\globalStorage\\moocfi.test-
my-code\\tmcdata\\TMC workspace\\Exercises\\hy\\hy-data-analysis-with-python-
2020\\part02-e04_word_frequencies\\src/alice.txt
First, I run
/bin/bash a.sh
It succeed.
But when I want to run
/bin/bash a.sh > a_info.txt
It failed.
Error: line 1: !#/bin/bash: No such file or directory
It does not seem that it’s '^M' that’s causing this error.
The shebang pattern is #!/bin/your_shell, not !#/bin/your_shell. Just fix the typo.
Check the file's encoding.
If you get this error, but you did not misspell the 'shebang', have a close look at the file's encoding. I was getting this error on a file with encoding 'UTF8 with BOM'... I guess the BOM (Byte Order Mark) was causing the problem. Changed the encoding to ASCII and that fixed it (or you can use 'UTF-8 without BOM' which is effectively the same for files only containing ASCII characters).
Another possible cause of the "No such file or directory" error is if your shell script is using CRLF instead of LF line endings.
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
recursively “normalize” filenames
Q on pastebin: http://pastebin.com/raw.php?i=19iYZpwY
i mean getting rid of special chars in filenames, etc.
i have made a script, that can recursively rename files [http://pastebin.com/raw.php?i=kXeHbDQw]:
e.g.: before:
THIS i.s my file (1).txt
after running the script:
This-i-s-my-file-1.txt
Ok. here it is:
But: when i wanted to test it "fully", with filenames like this [http://pastebin.com/raw.php?i=LQ07ntcS]:
¤¥¦§¨©ª«¬®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÂÃÄÅÆÇÈÊËÌÎÏÐÑÒÔÕ×ØÙUÛUÝÞßàâãäåæçèêëìîïðñòôõ÷øùûýþÿ.txt
áíüűúöőóéÁÍÜŰÚÖŐÓÉ!"#$%&'()+,:;<=>?#[]^_{|}~€‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ¡¢£.txt<br>
<br>
it fails [http://pastebin.com/raw.php?i=iu8Pwrnr]:<br>
$ sh renamer.sh directorythathasthefiles<br>
mv: cannot stat./áíüűúöőóéÁÍÜŰÚÖŐÓÉ!"#$%&\'()+,:;<=>?#[]^{|}~€‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ¡¢£': No such file or directorymv: cannot stat./áíüűúöőóéÁÍÜŰÚÖŐÓÉ!"#$%&\'()*+,:;<=>?#[]^{|}~€‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ¡¢£': No such file or directorymv: cannot stat./áíüűúöőóéÁÍÜŰÚÖŐÓÉ!"#$%&\'()+,:;<=>?#[]^_{|}~€‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ¡¢£': No such file or directorymv: cannot stat./áíüűúöőóéÁÍÜŰÚÖŐÓÉ!"#$%&\'()+,:;<=>?#[]^{|}~€‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ¡¢£': No such file or directorymv: cannot stat./áíüűúöőóéÁÍÜŰÚÖŐÓÉ!"#$%&\'()*+,:;<=>?#[]^{|}~€‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ¡¢£': No such file or directorymv: cannot stat./áíüűúöőóéÁÍÜŰÚÖŐÓÉ!"#$%&\'()+,:;<=>?#[]^_{|}~€‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ¡¢£': No such file or directorymv: cannot stat./áíüűúöőóéÁÍÜŰÚÖŐÓÉ!"#$%&\'()+,:;<=>?#[]^_`{|}~€‚ƒ„…†....and so on
$
so "mv" can't handle special chars.. :\
i worked on it for many hours..
does anyone has a working one? [that can handle chars [filenames] in that 2 lines too?]
Reading that script was almost painful...
For one, you should read this.
Then you should read about bash functions. After that you should read about sed and tr
Then you should consider this: do you really want to move the file each time that you perform a transformation on its name?
Then after all this thinking, you should come up with something a bit saner.
Wtf is going on your system? You should consider re setting up and pay attention on sane applicaitons and security.
However its very likely that you are just running into the max length of command arguments if i am looking at that.
If not, well install UTF8 locales and install them as system default.
On debian based systems this is usually just a matter of dpkg-reconfigure locales
also work on your accept rate.
I'm trying out CCNetConfig (warning, website a little slow). Great app, one annoyance.
I can see in the documentation and even in the source code (Look at the end of the Initialize method) that I should be able to pass in a command line parameter to automatically load the configuration file.
I have tried:
-f E:\CruiseControl.Net\server\ccnet.config
-file E:\CruiseControl.Net\server\ccnet.config
-f=E:\CruiseControl.Net\server\ccnet.config
-file=E:\CruiseControl.Net\server\ccnet.config
And the same 4 switches with quotes around the file name just in case.
They all produce errors or just don't work. Has anyone had success doing this?
Thanks in advance.
Try moving it to a different path. Try the simplest thing: c:\ccnet.config.
I'm guessing it's the source of the problem because I recall CCNetConfig gave me sh!t early on when I tried to open files in paths that had spaces in them (e..g in Program Files).
According to my version of CCNet the command line flag for using a different config file is -c or --config, so the following should do it...
-c "E:\CruiseControl.Net\server\ccnet.config"
...which works for me.
I don't know why it uses a non-standard flag, but there you go.