Thread: Python Shell Call does not work:

  1. #1
    Registered User
    Join Date
    Feb 2019
    Posts
    69

    Red face Python Shell Call does not work:

    Hey,

    Code:
        ...
        fprintf(python_file_pointer, "print(list)\n");
        system("python3 translation.py");
        fclose(python_file_pointer);
        fclose(fp);
    This pythoncode unfortunately does not get executed or the process is not started at all.

    Why?

    Thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Post the smallest and simplest compilable C program that demonstrates the problem, along with the test Python program.

    Actually, looking at the first fprintf call I'm guessing that you're writing the Python program from the C program, in which case you should fclose before calling system. But it's only a guess because you chose to post a code snippet without enough context.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    69
    Quote Originally Posted by laserlight View Post
    Post the smallest and simplest compilable C program that demonstrates the problem, along with the test Python program.

    Actually, looking at the first fprintf call I'm guessing that you're writing the Python program from the C program, in which case you should fclose before calling system. But it's only a guess because you chose to post a code snippet without enough context.
    Code:
    int main(int argc, const char * argv[]) {
        if(argc <= 2)return -1;
        
        char file_name[100];
        strncpy(file_name, argv[1], 100);
        FILE* fp = fopen(file_name, "read");
        if(!fp)return -1;
        
        //some irrelevant code
    
    
        remove("translation.py");
        FILE * python_file_pointer = fopen("translation.py", "ab+");
        fprintf(python_file_pointer, "list = []\n");
        
        //Do parsing and magic here, which does not matter
        
        fprintf(python_file_pointer, "print(list)\n");
        system("python3 translation.py");
        fclose(python_file_pointer);
        fclose(fp);
        
    }
    Problem solved! I have to close first! Thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How does a function call work?
    By Ramcin Oudishu in forum C Programming
    Replies: 2
    Last Post: 04-16-2014, 05:24 PM
  2. Can't get sample shell code to work...
    By jcafaro10 in forum C Programming
    Replies: 6
    Last Post: 07-07-2010, 08:04 AM
  3. cut command wont work in shell??
    By vaibhavs17 in forum Linux Programming
    Replies: 3
    Last Post: 03-03-2010, 08:52 AM
  4. Shell callable from Python
    By ITAmember in forum Linux Programming
    Replies: 5
    Last Post: 03-19-2009, 04:22 PM
  5. Getting boost.python to work
    By h3ro in forum C++ Programming
    Replies: 7
    Last Post: 12-16-2008, 07:54 PM

Tags for this Thread