I’m trying to use Python dump a SQLite database into a 7-zip archive (under Windows). I have the following code:
import os
import subprocess
DatabaseName = os.path.join('C:\\','Database.sqlite')
ArchiveName = os.path.join('C:\\','temp.7z')
FileName = 'trial.txt'
command = 'sqlite3 %s < C:\\\\SQLite3\\DumpToSTDout.txt | 7z a %s -si%s' % (DatabaseName, ArchiveName, FileName)
print command
DumpProc = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
DumpProc.wait()
print DumpProc.stdout.read()
print DumpProc.stderr.read()
C:\Sqlite3\DumpToStdout.txt is a text file with ‘.output stdout’ on the first line and ‘.dump’ on the second line.
When I run this code I get the following error:
sqlite3: Error: too many options: "C:\\SQLite3\DumpToSTDout.txt"
Use -help for a list of options.
But if I take the printed command and run it directly in a command window, it runs correctly.
What am I doing wrong?
Since you are using shell-specific characters (
<and|) you need to run the command withshell=Truein thePopen()call.