0

I am very new to Python and I have been trying to find a way to write in cmd with python.

I tried os.system and subprocess too. But I am not sure how to use subprocess.

While using os.system(), I got an error saying that the file specified cannot be found. This is what I am trying to write in cmd os.system('cd '+path+'tesseract '+'a.png out')

I have tried searching Google but still I don't understand how to use subprocess.

EDIT: It's not a problem with python anymore, I have figured out. Here is my code now.
os.system("cd C:\\Users\\User\\Desktop\\Folder\\data\\") os.system("tesseract a.png out")

Now it says the file cannot be open. But if I open the cmd separately and write the above code, it successfully creates a file in the folder\data.

wingerse
  • 3,670
  • 1
  • 29
  • 61
  • To make text appear on the command line, I suggest the `print` statement. – Kevin Aug 25 '14 at 15:40
  • 1
    You're correctly writing to the command line with `os.system`. That's not the problem. You're just getting an error because command line isn't sure how to process your command because as it says, that's not the correct path to whatever file you're trying to use. – Charles Clayton Aug 25 '14 at 15:44
  • It's the correct path. If I open the cmd separately and write `cd desktop\folder tesseract a.png out`, it will make a text file in that folder. I don't know why python is not doing that. :/ – wingerse Aug 25 '14 at 15:58
  • I guess it's because I wrote the tesseract part in the `cd` line. How dumb I am. Anyways, thanks . I figured it out – wingerse Aug 25 '14 at 16:02
  • Before your command, print path and tesseract so I can see what they are. What I think is probably the problem is you have backslash escape characters in the string python isn't reading them the way you want them to. Try replacing all instances of \ in your string with \\. – Charles Clayton Aug 25 '14 at 16:03
  • Tesseract is an OCR Engine. First I will have to change cmd directory to the folder with the image. Then on the cmd line `tesseract image.png out` will make a text file called 'out.txt' on that folder, with the text in the image. That's what I am trying to do. – wingerse Aug 25 '14 at 16:13
  • For `subprocess`, read here: http://stackoverflow.com/questions/4813238/difference-between-subprocess-popen-and-os-system – Reuben L. Aug 26 '14 at 10:33

1 Answers1

2

Each call to os.system is a separate instance of the shell. The cd you issued only had effect in the first instance of the shell. The second call to os.system was a new shell instance that started in the Python program's current working directory, which was not affected by the first cd invocation.

Some ways to do what you want:

1 -- put all the relevant commands in a single bash file and execute that via os.system

2 -- skip the cd call; just invoke your tesseract command using a full path to the file

3 -- change the directory for the Python program as a whole using os.chdir but this is probably not the right way -- your Python program as a whole (especially if running in a web app framework like Django or web2py) may have strong feelings about the current working directory.

The main takeaway is, os.system calls don't change the execution environment of the current Python program. It's equivalent to what would happen if you created a sub-shell at the command line, issued one command then exited. Some commands (like creating files or directories) have permanent effect. Others (like changing directories or setting environment variables) don't.

Chris Johnson
  • 20,650
  • 6
  • 81
  • 80