Can you describe better a bigger picture of what you really want to do. Can you create some.bat that does something and to describe what the Python program should do? Should the user work interactively with it?

So basically what I want to do is create a py script that will call a cmd prompt to navigate to c:\program files\microsoft office\Office v# and call upon msaccess.exe and then call upon a command line switch to compact and repair a database. CMD LINE syntax is below.

@ltpitt: Be careful with the line 5. Backslashes must be doubled or the literal should be prefixed as raw string like this r'C:\Program files\Microsoft Office'. Actually, it is more usual and less error prone to use normal slashes as in UNIX based systems -- it works also for Windows. So, 'C:/Program files/Microsoft Office'.

@sj77: The double quotes and single quotes for string literals are the same. You can choose. But single quotes are more native for Python. The quotes are not part of the string value.

At the line 10 the 'c:\\my_database.mdb' is passed as the argument. The mdb_path is a variable that contains that value. The + does a string concatenation here. No quotes are added to the value. So, occasionally, you may need to add double quotes inside the value if the called command line utility requires them -- if you pass the command as a string. There is also alternative way when you pass the command as the list of parts (as if parsed from command line).

Use a commander or the File Explorer to see whether your Office tools are really located at 'C:/Program Files/Microsot Office/Office15'. It can possibly be 'c:/Program Files (x86)/Microsoft Office/Office1X' (notice the (x86) if you have a 32-bit version), and/or possibly a different version of Office instead of 1X.

For understanding better... Notice that the msaccess.exe was given the full path and stored in the pgm variable.

The command variable is created as concatenation of the pgm, space, and the argument (the database file name with the path) in mdb_path -- the same way you would write it to command line.

There is probably no need to switch to the directory where the msaccess.exe is. However, notice that the pgm variable content is wrapped in double quotes inside -- single quotes outside (so, the double quotes are part of the value of the string). Otherwise, it would complain that "C:\Program" utility was not found. That is because the cmd (in the role of the shell) is going to interpret the string.

Because of the complications, it may be better to avoid using the shell (cmd), and pass the program name and the argumens as if after parsing by cmd. Then the program would look like:

Notice that there is no shell=True (it is False by default). This way, the msaccess.exe will be launched directly, not via another cmd. But it also means that you have to tell the full path, because it is the cmd who adds the full path otherwise (searching the program in the PATH paths). Notice also, that there is no need to add the double quotes.

Notice also, that the msaccess with the argument is not passed as one string (the program separated from the argument by space). Instead, the list (in square brackets), is passed with the two element. The first element tells the full path to the executable, the second element tells the argument.

In command line I would have gone to cd "C:\program files\microsoft office\office15" > once in the path I would call upon the msaccess.exe> then used the two switches I need> /compact /repair and then the file path of the MDB> msaccess.exe /compact /repair "C:\Test\AR001.mdb"

The formatting is not that important here -- just to make it more visible. Notice the comma that separates the added elements in the list.

My guess is that the function should have the switches always used for the msaccess. However, it is also possible to pass them via arguments of the function. Now, not exactly for the switches, but to show some extra argument. Let's say, that you want to enhance the function so that it was possible to pass a different version of msaccess.exe (location). Let's say, you may have a different function (implemented in future) that would get the location of msaccess.exe dynamically, somehow. You can think in advance and rewrite (the fancy word is to "refactor") the code like this:

Notice the second argument of the function. The argument is assigned by the full name of msaccess.exe that you use now. Because it is assigned a default value, you do not need to write the argument when calling the function. But in future, you can pass another value to the argument.

Now notice that the subprocess.check_output uses the variable msaccess as the first element of the list. The other elements (two switches and the database file) are passed the same way. Possibly, it is more readable. At least it is not more difficult to understand.

You should always try to make your source as readable as possible. It may happen, that you will look at it few months/years later. You will be pleased if you will be able to see exactly how it works without the need to decipher the meaning. If it looks better than previous version, it may a be better solution.

Now notice that the code is so simple, that introduction of the result variable does not make it simpler. (It may make it simpler in more complex situations, but this is not the case.) So, you can even simplify it:

The string in trippled double quotes (this is the convention) documents the functionality. Some editors are able to display the string when you type the function name (when you want to call it). Some other tools will get the string and will generate the documentation for your program. (It makes sense when the project gets bigger.)

Featured Post

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Flask is a microframework for Python based on Werkzeug and Jinja 2. This requires you to have a good understanding of Python 2.7.
Lets install Flask!
To install Flask you can use a python repository for libraries tool called pip. Download this f…

The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…

Learn the basics of if, else, and elif statements in Python 2.7.
Use "if" statements to test a specified condition.:
The structure of an if statement is as follows:
(CODE)
Use "else" statements to allow the execution of an alternative, if the …

Learn the basics of while and for loops in Python.
while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows: while <condition>: do something repeate: The break statement m…