Using Windows Command Line with Python Subprocess Module - cmd

So basically I've got the worst situation possible on Windows... A path with spaces on a drive other than C:/
I'm trying to run the following:
print(subprocess.check_call("cd d: && d: && " + '"' + dir_name + '"', shell=True))
Where dir_name is the name of the directory plus the name of the executable file. The way I get dir_name is as follows:
dir_path = os.path.dirname(os.path.realpath(__file__))
dir_name = (dir_path.replace("\\","/") + "/bowtie2-2.3.4-mingw-x86_64/bowtie2-build").replace(" ", "\\ ")
I've also tried a few different variants of dir_name:
dir_path + "/bowtie2-2.3.4-mingw-x86_64/bowtie2-build"
dir_path.replace("\\","/")+"/bowtie2-2.3.4-mingw-x86_64/bowtie2-build"
I've tried it with and without quotes surrounding the whole thing, I've tried it with and without the shell option, I've tried it in cmd.exe and basically every time I get something along the lines of:
The system cannot find the path specified.
The full path is essentially:
D:/My Documents/2018 Documents/Class Name/Assignment Name/Subfile/ProgramName
With the variants I've tried being:
D://My Documents//2018 Documents//Class Name//Assignment Name//Subfile//ProgramName
D:/My\ Documents/2018\ Documents/Class\ Name/Assignment\ Name/Subfile/ProgramName
D://My\ Documents//2018\ Documents//Class\ Name/Assignment\ Name//Subfile//ProgramName
"D:/My Documents/2018 Documents/Class Name/Assignment Name/Subfile/ProgramName"
"D:/My\ Documents/2018\ Documents/Class\ Name/Assignment\ Name/Subfile/ProgramName"
"D://My\ Documents//2018\ Documents//Class\ Name//Assignment\ Name//Subfile//ProgramName"
I'm up for anything at this point, thank you so much in advance!
EDIT #1:
Based on comments, I should add that I'm trying to run bowtie2-build which is an extensionless program in the Bowtie package. It takes two command-line arguments, an input file and an output file name.

Related

How to write output file of ruby code into specific directory/location?

I have ruby code which will create output file, currently file is creating at location from where my script is running.
I have to write output file to different location so i am specifying explicit path into code. but it's not able to create file. my code looks like :
fname = "C:\repo\cookbooks\abc\recipes\add.rb"
somefile = File.open(fname,"w")
somefile.puts "end"
somefile.close
If i specify
fname = "add.rb"
it's working but i want to create it at different location as i mentioned above code in C:\ drive.
Because \ in strings are special characters so you should use \\ (double backslashes) to get a single backslash. But there is a better way, you don't need to deal with backslashes at all:
fname = File.join("C:", "repo", "cookbooks", "abc", "recipes", "add.rb")

Using ls to launch a Python script on each subdirectory

I created this little Python script Essai_Bash.py to make some tests:
#!/usr/bin/python
import argparse
import os
parser = argparse.ArgumentParser()
parser.add_argument('-i', action='store', dest='InputDir', help='Working Directory') # Empty folders for outputs.
parser.add_argument('--version', action='version', version='%(prog)s 0.1')
results = parser.parse_args()
print 'Current input directory =', results.InputDir
dir_path=str(os.path.abspath(results.InputDir)) # Retrieving an output folder name to use as species ID:
path,CodeSp = os.path.split(dir_path)
print "Currently working on species: "+str(CodeSp)
Back to my shell, I type the following command, expecting my script to run on each directory that is present in my "Essai_Bash" folder:
listdir='ls ../Main_folder/' # I first used backtips instead of simple quotes but it did not work.
for dir in $listdir; do ./Essai_Bash.py -i ../Main_folder/$dir; done
I am surely missing something obvious but it does not work. It seems like $listdir is considered as a characters strings and not a list of directories. However, just typing $listdir in my shell actually gives me this list!
Just use glob extension, parsing ls output is not safe.
Also dir variable already contains ../Main_folder/
listdir=( ../Main_folder/*/ )
for dir in "${listdir[#]}"; do ./Essai_Bash.py -i "$dir"; done

"Can't open file "C:" for reading; you may not have read permission." error in MATLAB

I have such a code;
for x = 1:100
path = sprintf('C:\Users\hasan_000\Documents\MATLAB\Project\Images\%d.jpg', x);
imgarray = imread(sprintf(path));
end
I have a folder involves 100 pictures. I want to convert them to matrix by uploading automatically in a loop.
But I get this error:
Can't open file "C:" for reading;
you may not have read permission.
How can I fix the problem?
Thanks,
The code should output the warning:
"Warning: Escape sequence '\U' is not valid. See 'help sprintf' for valid escape
sequences. "
You need to escape the \ when using sprintf. With yor code path is C:. For examples how proper escaping is done, please check the documentation for sprintf. Instead I would use this code:
P=fullfile('C:\Users\hasan_000\Documents\MATLAB\Project\Images',sprintf('%d.jpg',x))
imgarray = imread(P);
sprintf('C:\\Users\\hasan_000\\Documents\\MATLAB\\Project\\Images\\%d.jpg', x); should solve the issue.
sprintf('%s%d%s','C:\Users\hasan_000\Documents\MATLAB\Project\Images\',x,'.jpg');
is what I would suggest as it makes the code more intuitive and readable.
sprintf does not like your backslashes \ in the filename since it can be part of a specific command. If you simply run the path file you'll see:
path = sprintf('C:\Users\hasan_000\Documents\MATLAB\Project\Images\%d.jpg', 1);
path = C:
So that's where your code breaks. I'm currently not sitting on a windows machine, but I'd try reversing the slashes from backslashes \ to normal ones / and see if it can open that.
Second method works for sure:
path = ['C:\Users\hasan_000\Documents\MATLAB\Project\Images\', sprintf('%d.jpg', x)]
path = C:\Users\hasan_000\Documents\MATLAB\Project\Images\1.jpg

What does slash dot refer to in a file path?

I'm trying to install a grunt template on my computer but I'm having issues. I realized that perhaps something different is happening because of the path given by the Grunt docs, which is
%USERPROFILE%\.grunt-init\
What does that . mean before grunt-init?
I've tried to do the whole import manually but it also isn't working
git clone https://github.com/gruntjs/grunt-init-gruntfile.git "C:\Users\Imray\AppData\Roaming\npm\gru
nt-init\"
I get a message:
fatal: could not create work tree dir 'C:\Users\Imray\AppData\Roaming\npm\.grunt-init"'.: Invalid argument
Does it have to do with this /.? What does it mean?
The \ (that's a backslash, not a slash) is a directory delimiter. The . is simply part of the directory name.
.grunt-init and grunt-init are two distinct names, both perfectly valid.
On Unix-like systems, file and directory names starting with . are hidden by default, which is why you'll often see such names for things like configuration files.
The . is part of a directory name. Filenames can contain . . The \ is a separator between directory names.
Typically, files or directories starting with . are considered "hidden" and/or used for storing metadata. In particular, shell wildcard expansion skips over files that start with ..
For example if you wrote ls -d * then it would not show any files or directories beginning with . (including . and .., the current and parent directories).
Linux hides files and directories whose names begin with dot, unless you use the a (for "all") option when listing directory contents. If this convention is not followed on Windows, your example is probably just a carryover.
It may well be something behind the scenes (later) expects that name to match exactly. While I like things, installers, for example, to just do what I said, I realize that keeping default value is the most tested path.
Directories starting with a dot are invisible by default on xNIX systems. Typically used for configurations files and similar in a users home directory.
\ before " has a special meaning on windows, the error is because windows won't let you create a file containing " as part of its name.

Run makefile if it exists, otherwise run extension specific command with vimscript

I want to map F9 to run the makefile in the current or parent directories, and if no makefile is found, run something based on the file extension.
I tried doing this:
function! Runf9()
if filereadable("./Makefile")
make
elseif (&filetype == "tex")
execute("!pdflatex " + bufname("%"))
endif
endfunction
nmap <silent> <F9> :call Runf9()<CR>
But this only works for makefiles in the current directory, I don't know how to search for them in the parent directories, it also does not work for .tex files when there is no Makefile.
If the + in the line with execute is exchanged with a . it works.
You can do (both up- and downward-) file searches via the findfile() function.
As you've already found out, string concatenation has to be done with ., not with +, which is for numerical addition only.

Resources