0

In the below code, I am trying to take the path name (inclusive of file name) and converting them to another path name with text file.

Now I would like to pass both variable as argument for another py which will take it as argument

import os

thisdir=[os.path.join(r,file) for r,d,f in 
os.walk("C:\\Users\\vnitin\\OneDrive - NetApp Inc\\IDP\\Files\\") for file 
in f]
for i in range(len(thisdir)):
    text_path = thisdir[i].replace('pdf', 'txt')
    print(text_path)
    os.system('py pdf2txt.py -o text_path thisdir[i]')

But individual command for pdf2txt.py works very well.

py .\pdf2txt.py -o 'C:\Users\vnitin\OneDrive - NetApp Inc\IDP\Files\11.txt'  
'C:\Users\vnitin\OneDrive - NetApp Inc\IDP\Files\11.pdf'
2
  • Perhaps you should have a look to the subprocess module: docs.python.org/3/library/subprocess.html#module-subprocess Commented Apr 9, 2018 at 8:57
  • 1
    os.system('py pdf2txt.py -o text_path thisdir[i]') this executes as a string, so your i is not what you expect it to be Commented Apr 9, 2018 at 9:00

3 Answers 3

1

since thisdir[i] is not converted to its value during execution, so the error No such file or directory

Replace os.system('py pdf2txt.py -o text_path thisdir[i]') with

os.system("python ./pdf2txt.py -o {0} {1}".format(text_path,thisdir[i]))

Sign up to request clarification or add additional context in comments.

4 Comments

No it doesn't I am getting IOError: [Errno 2] No such file or directory: 'thisdir[i]'
Now this. IOError: [Errno 13] Permission denied: 'C:\\Users\\vnitin\\OneDrive' Not sure, why permission denied, because it works very well with the full path as argument.
Unsure of the permission denied error, can you check the file permissions?
Hi. it worked. I gave some better path and it went fine. Thanks a lot for quick help :)
0

Two Solutions: 1)You can directly import and call the main method with params

data = pdf2txt.main(['pdf2txt.py',filename])

or

2) Form a string and pass it as a command

    string ="py .\pdf2txt.py -o" +" C:\Users\vnitin\OneDrive - NetApp Inc\IDP\Files\11.txt"+" C:\Users\vnitin\OneDrive - NetApp Inc\IDP\Files\11.pdf"

1 Comment

about the first point, after import pdf2txt and running the first option, it is giving error ImportError: cannot import name 'maxint
0

Use SubProcess module

import subprocess
subprocess.Popen("python ./pdf2txt.py -o {0} {1}".format(text_path,thisdir[i])", shell=True)

Now, stdin, stdout, and stderr can also be redirected.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.