Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4065694
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T16:00:58+00:00 2026-05-20T16:00:58+00:00

Am using subprocess on Windows and Python 2.6 as follows. I am trying to

  • 0

Am using subprocess on Windows and Python 2.6 as follows. I am trying to parse a text file using a legacy parser application (assume parser.py) as follows:

import subprocess
k = subprocess.Popen(['python', 'parser.py', '-f C:\Report1\2011-03-14.txt'],
                     shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
print k.communicate()

The issue here is with the way filename gets passed to the legacy application where I cannot change the code but only can access it using Python.

It generates with the following error:

IOError: [Errno 22] invalid mode (\'r\') or filename: C:\\Report1\\2011-03-14.txt

When I copy the modified filename(with double forward slashes) from the traceback to check the existence, the system is not able to find it.

Question: How can I pass the path as argument so that it gets treated without getting changed to double slashes so that the system can read the file?

NOTE: os.sep also does not resolve the issue.

EDIT: Executing using os.system works perfectly, but the issue there is to grab the output for later use. Am currently using os.sytem in a module(run_parser.py) and then using subprocess in another module(get_parse_status.py) that Popens run_parser.py to grab the output. Would appreciate anything that is better than this.

Thanks for the time.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T16:00:59+00:00Added an answer on May 20, 2026 at 4:00 pm

    Change your parameter list to encode the path as a raw string:

    k = subprocess.Popen(['python', 'parser.py', '-f', r'C:\Report1\2011-03-14.txt'],
                         shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    

    a simple program that reads a file and reports the length:

    import sys
    import os
    userinput = sys.argv[1]
    data = open(userinput, 'rb').read()
    datalength = len(data)
    fname = os.path.basename(userinput)
    print "%s datasize = %s" % (fname, datalength)
    

    Then to call it through the interpreter:

    >>> k = subprocess.Popen(['python', 'test2.py', 'w:\bin\test2.py'], shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    >>> k.communicate()
    5: ('Traceback (most recent call last):\r\n  File "w:\\bin\\test2.py", line 4, in <module>
    data = open(userinput, \'rb\').read()
    IOError: [Errno 22] invalid mode (\'rb\') or filename: 'w:\\x08in\\test2.py', None)
    >>> k = subprocess.Popen(['python', r'w:\bin\test2.py', r'w:\bin\test2.py'], shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    >>> k.communicate()
    6: ('test2.py datasize = 194\n', None)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a python program that lauches a subprocess (using Popen). I am
I'm using Python's subprocess.Popen to perform some FTP using the binary client of the
Windows version of Python 2.6.4: Is there any way to determine if subprocess.Popen() fails
When I execute a python script using subprocess.Popen(script, shell=True) in another python script, is
How do I execute the following shell command using the Python subprocess module? echo
Python's subprocess module by default passes all open file descriptors to any child processes
I'm trying to run console commands via subprocess.Popen , and whenever I run it
I'm trying to build a C++ extension for python using swig. I've followed the
I am Using Python 2.7.1 on a Windows Server 2008 R2 x64 box. I'm
So, I have been facing a problem with using subprocess for a python app

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.