I have 3 files all within the same directory:
I want to execute my awk file using subprocess so I can call it within a PyTest testcase in the future. These are my sample files:
ex.awk
#!/usr/bin/awk -f
BEGIN {
FS = ",";
}
{
print NF;
}
END {print "DONE"}
data.csv
10,34,32
4,76,8304
4759,5869,2940
file.py
import subprocess
cmd = ['awk', '-f', 'ex.awk', 'data.csv']
subprocess.run(cmd)
On bash, the following command works:
$ awk -f ex.awk data.csv
3
3
3
DONE
I'm trying to get the same result when running file.py, but I get the following
FileNotFoundError: [WinError 2] The system cannot find the file specified
Am I missing some args in my array? Did I need to use some other subprocess args like shell
or others?
To execute the awk script from Python on Windows where you have encountered a FileNotFoundError
, it's often due to the way Windows handles file paths or the execution of Unix/Linux-specific utilities that might not be directly available in the Windows command prompt environment. So you need to provide the absolute path or update Environment variable path to make it available.
Try these after adjusting the 'C:/Program Files/Git/usr/bin/awk.exe' path as necessary;
import subprocess
cmd = ['C:/Program Files/Git/usr/bin/awk.exe', '-f', 'ex.awk', 'data.csv']
subprocess.run(cmd, shell=True)
Alternatively, to execute it within a Unix-like shell provided by Git Bash or WSL:
import subprocess
cmd = 'awk -f ex.awk data.csv'
subprocess.run(cmd, shell=True, executable='C:/Program Files/Git/bin/bash.exe')