0

I have two main python scripts in my project:

sftpConnector.py and fileIterator.py

sftpConnector.py establishes connection to an SFTP server and downloads some file, it however does not call any other script. It has only function: establisher()

While, fileiterator.py also has one function iterator() it calls two other scripts: folderGenerator.py and dataProcessor.py()

When independently called, both the scripts, sftpConnector.py and fileIterator.py execute properly without any issues, however, when I try to create a main.py file to call these two scripts one after the other, it does not work.

The main.py looks like this:

import sftpConnector
import fileIterator


if __name__ == 'main':
    folder_name = input('File name:\n')
    print('folder_name')

    sftpConnector.establisher(folder_name)

    fileIterator.iterator(folder_name)

This main.py file runs the first script and function, sftpConnector.establisher(folder_name) however it does not even go inside the second script. I am not sure what's going on here.

Also, not sure if this will help, but neither of the script are returning anything.

6
  • Does sftpConnector.establisher() ever actually return? Until that happens, the next line of the main script isn't going to execute. Commented Jul 21, 2022 at 14:14
  • There is no return statements in either one of the script, the sftpConnector downloads some files from the server and stores it in local directory. the second file, fileiterator.py is supposed to use these downloaded files to call other functions and do the processing. Commented Jul 21, 2022 at 14:38
  • standard procedure: insert print statements to see where a script hangs. Consider providing a minimal reproducible example. Commented Jul 21, 2022 at 14:44
  • check / correct code indentation. Commented Jul 21, 2022 at 14:45
  • 1
    I would argue that your script isn't running, at all. "__main__" not "main". Commented Jul 21, 2022 at 14:56

1 Answer 1

1

Your run guard should be

if __name__ == "__main__":

That’s how to ensure that your code only runs if run as a module (versus being imported). You accidentally checked for the wrong name (“main”) and I’m guessing that’s why the code in your if block isn’t running. It’s an easy mistake to make as it’s easy to assume that __name__ refers to the name of your file when you run your script directly, but it doesn’t; it refers to the environment where the toplevel code is run. The line if __name__ == '__main__': is asking the question: “Is this script the toplevel one?”

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

3 Comments

@OneMadGypsy thanks for fixing my quote characters. I’m on my phone and was having trouble with that.
I hope you don't mind. I added a little info to the end of your answer. Your answer is already correct. IMO, it's better to butter it up a little than create an identical answer that just includes 3 extra sentences of info.
I think that’s fine. I may edit it a bit to fit the style of the rest of my answer.

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.