Parsing arguments to a python file

  • Context: Python 
  • Thread starter Thread starter magistros
  • Start date Start date
  • Tags Tags
    File Python
Click For Summary

Discussion Overview

The discussion revolves around how to pass arguments to a Python script, specifically within the context of using CMS_SW. Participants explore methods for handling input files in Python scripts and express challenges encountered during implementation.

Discussion Character

  • Technical explanation, Homework-related, Debate/contested

Main Points Raised

  • One participant seeks assistance on how to declare input files in a Python script when using the command "cmsRun script.py file.root".
  • Another participant suggests using the sys module to access command-line arguments, providing a basic code snippet for opening the input file.
  • A different participant mentions the argparse module as a potential solution for handling command-line arguments, indicating it may be useful depending on the complexity of the needs.
  • A participant reports an error encountered when attempting to run the script, suggesting that CMS_SW may not support Python as expected.
  • Another participant notes that they encountered an issue with the argparse module being missing and expresses a decision to switch to C++ for their implementation.

Areas of Agreement / Disagreement

Participants express differing experiences and challenges with the Python implementation, and there is no consensus on a definitive solution. Some participants propose methods while others report failures or limitations.

Contextual Notes

Limitations include potential misunderstandings about the capabilities of CMS_SW regarding Python, as well as issues with missing modules like argparse that may affect the proposed solutions.

magistros
Messages
4
Reaction score
0
Dear forum,
I would like you to help me on .py scripts.
I am using CMS_SW and I want to do something like this:

cmsRun script.py file.root

where "file.root" is the file that the .py script needs to run... But I cannot find, how I declare it to the .py code...
these .py files, are configuration files...

thank you in advance!
 
Technology news on Phys.org
Try it like this:

Code:
import sys
cmd=sys.argv
filename=cmd[1]
infile = open(filename, 'r') # Now do what you want with the file
 
Depending on the complexity of your needs, you may want to look at the argparse module.
 
Dear phyzguy,
I tried it but I get this error
cms::Exception caught in cmsRun
---- Configuration BEGIN
FailedInputSource Configuration of primary input source has failed
---- Configuration END

Maybe the CMS_SW does not run python but something like this...

Dear jhae2.718,
I tried the argparse and it says that the argparse is missing...

Anyway, I will make it in C++..

Thank you for your time guys!
 

Similar threads

Replies
6
Views
3K
  • · Replies 14 ·
Replies
14
Views
8K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 14 ·
Replies
14
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 17 ·
Replies
17
Views
7K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 9 ·
Replies
9
Views
2K
Replies
5
Views
16K