Use python, to open cmd window with the directory supplied as an argument as the current directory - python-3.x

I want to use Python to open a directory in a Win10 cmd window, and keep the window open.
I made a batch file named: open_dir_in_cmd_window.CMD:
CD /D %1
I tested that batch file successfully, by creating another batch file named, Test.cmd:
Rem "open_dir_in_cmd_window.CMD" "f:\backup"
"open_dir_in_cmd_window.CMD" "f:\backup"
A very helpful webpage provides the following example, which I seem unable to follow correctly:
Spaces in Program Path + parameters with spaces:
CMD /k ""c:\batch files\demo.cmd" "Parameter 1 with space" "Parameter2 with space""
I made a python script, which contains the following lines, which alas, triggers an error message:
import subprocess
subprocess.run(cmd /k "E:\open_dir_in_cmd_window.CMD f:\backup")
When I open a Command Prompt window and run:
"C:\ProgramData\Anaconda3\python.exe" E:\open_dir.py
I get an error message, SyntaxError: invalid syntax, with this:
subprocess.run(cmd /k "E:\open_dir_in_cmd_window.CMD f:\backup")
^
I've tried many different permutations of double quoting and can't figure out the right way to do it.
I have spent many hours hunting on the web and trying to figure this out and I do not know what to do.
Any suggestions would be appreciated.

Related

How to enter windows paths to run Python in Sublime Text 3 using Ctrl+B

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

Why won't my batch file run? Syntax Error

I keep getting a syntax error against the "C" in "C:\Users".
`#! python3
print('Hello world')`
#C:\Users\AK\MyPythonScripts>py.exe hello.bat
# File "hello.bat", line 1
# #py C:\User\AK\MyPythonSctipts\hello.py %*
# ^
#SyntaxError: invalid syntax
I've tried adding "\". I can't figure it out. Following along in "Automate the Boring Stuff." (Lesson 22)
#I keep getting a syntax error against the "C" in "C:\Users".
`#! python3
print('Hello world')`
#C:\Users\AK\MyPythonScripts>py.exe hello.bat
# File "hello.bat", line 1
# #py C:\User\AK\MyPythonSctipts\hello.py %*
# ^
#SyntaxError: invalid syntax
I expect to run the batch file and added a "#pause" feature, but the program won't execute since "C" is an invalid syntax.
May not the perfect answer you are looking for, but let me share in general essense how you run a python script in windows.
Open Command line: Start menu -> Run and type cmd
Type: C:\python27\python.exe C:\Users\Awesome\Desktop\Adel.py
In your system, your python .exe location might a bit different or you could be using python3, but overall format is specify you python exe first followed by your script's full path (<python.exe> <python-script-full-path>
I figured it out! My command prompt path wasn't set properly. I went to "My Computer" -> right-click -> Properties -> Advanced System Settings and changed my environment variable.

An error when loading a 2mb dataset of floating points (python)

Does any one know why i got an error of "FileNotFoundError: [Errno 2] No such file or directory: 'bcs.xlsx'" when i'm loading this file of size 2mb it has around 60,000 rows and 4 columns.
i tried using csv instead of xlsx but i get the same error and i've checked hundreds times that the script and the file are at he same directory.
This is because Python does not find your file, errors are not lying.
But there's a misunderstanding in your question, you checked that the file is in the same directory as your script, but that's not the check you have to do. You have to check the file is in the current working directory of your python script.
To see your current working directory, use:
import os
print(os.getcwd())
And as we're at it you can list this directory:
print(os.listdir())
I don't know how you execute your script, but if you're using a terminal emulator, a typical way to give a file name to a program is by argument, not hardcoding its name, like by using argparse. And if you do this way, your shell completion may help you naming your file properly, like:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('file', type=argparse.FileType('r'))
args = parser.parse_args()
print(args.file.read())
Now on a shell if you type:
python3 ./thescript.py ./th[TAB]
your shell will autocomplete "./th" to "./thescript.py" (if and only if it exists), highly reducing the probablity of having a typo. Typically if there's a space in the filename like "the script.py", your shell should properly autocomplete the\ script.py.
Also if you use argparse with the argparse.FileType as I did, you'll have a verbose error in case the file does not exist:
thescript.py: error: argument file: can't open 'foo': [Errno 2] No such file or directory: 'foo'
But… you already have a verbose error.

Why am I getting a file not found error in Slimv?

I've set up slimv with the following command in vim:
let g:slimv_swank_cmd = '!gnome-terminal -e "sbcl --load ~/.vim/bundle/slimv/slime/start-swank.lisp &"'
When opening .lisp files and starting slimv, I recieve the following error message in the newly opened terminal window:
debugger invoked on a SB-INT:SIMPLE-FILE-ERROR in thread
#<THREAD "main thread" RUNNING {1002A8B203}>:
Couldn't load #P"~/.vim/bundle/slimv/slime/start-swank.lisp": file does not exist.
Type HELP for debugger help, or (SB-EXT:EXIT) to exit from SBCL.
restarts (invokable by number or by possibly-abbreviated name):
0: [CONTINUE] Ignore runtime option --load "~/.vim/bundle/slimv/slime/start-swank.lisp".
1: [ABORT ] Skip rest of --eval and --load options.
2: Skip to toplevel READ/EVAL/PRINT loop.
3: [EXIT ] Exit SBCL (calling #'EXIT, killing the process).
(LOAD #P"~/.vim/bundle/slimv/slime/start-swank.lisp" :VERBOSE NIL :PRINT NIL :IF-DOES-NOT-EXIST T :EXTERNAL-FORMAT :DEFAULT)
However, running the sbcl command from the terminal works properly because the file does exist on the path specified. How can I fix this issue?
You need to make sure that the tilde gets expanded. Expansion is provided by the shell.
You can also compute the pathname in Common Lisp using:
(merge-pathnames ".vim/bundle/slimv/slime/start-swank.lisp"
(user-homedir-pathname))
-> #P"/home/foobar/.vim/bundle/slimv/slime/start-swank.lisp"
Merging a relative pathname with an absolute pathname, adds the directory like above.
Both functions (merge-pathnames and user-homedir-pathname) are in the ANSI CL standard.

How do I get rid of errors in .bat file that aren't affecting outcome of the script?

I have an application calls the below command:
C:\Users\212340141>"C:\Program Files\Microsoft Office\Office14\excel.exe" /e "C:
\My Programs\CPU Analysis\data\IOParse.xlsm" "-iodumplocation"C:\My Programs\CPU
Analysis\iodump\065901_iodump.txt""
When I run this command, it opens excel, passing in the C:\My Programs\CPU Analysis\iodump\065901_iodump.txt path as a parameter for the excel book to use in the macros it runs automatically. The macros run correctly and the file is modified correctly, but I get the following errors when I run the command:
How can I get rid of the errors?
Answer
As mentioned in the accepted answer, excel was trying to open multiple files because of how I wrote the command. The way I solved this is I created a text file to hold the path I was trying to pass into the macro. The macro would open the text file and read the path to get the path it needed. This is much cleaner and easier than trying to get the path from the command line.
Description of the startup switches for Excel lists and describes the optional switches which can be used on starting Excel. /e is listed on this page written by Microsoft. But -iodumplocation is definitely not a command line switch for Excel.
Using double quotes within a double quoted string is always a mistake on command line and the result is unpredictable as depending on code of command line parser, see answer on Why double quotes should be always only at beginning and end of an argument string?
The command line used is obviously interpreted by Excel as
"C:\Program Files\Microsoft Office\Office14\excel.exe" /e "C:\My Programs\CPU Analysis\data\IOParse.xlsm" "-iodumplocation" C:\My Programs\CPU Analysis\iodump\065901_iodump.txt""
which results in
/e ... starting without displaying the startup screen and without the creation of a new workbook.
C:\My Programs\CPU Analysis\data\IOParse.xlsm ... opening this marco-enabled workbook file.
-iodumplocation ... the failed attempt to open a file with name -iodumplocation.xlsx in current working directory.
C:\My ... the failed attempt to open a file with name My.xlsx in root of drive C:.
Programs\CPU ... the failed attempt to open a file with name CPU.xlsx in subdirectory Programs of current working directory.
Analysis\iodump\065901_iodump.txt"" ... the failed attempt to open a file with invalid name 065901_iodump.txt"" in subdirectory Analysis\iodump of current working directory.
I can't suggest a correct command line as I don't know what -iodumplocation should be.

Resources