खोज…


परिचय

Sys मॉड्यूल प्रोग्राम के रनटाइम वातावरण से संबंधित फ़ंक्शन और मान तक पहुंच प्रदान करता है, जैसे कि sys.argv या फ़ंक्शन sys.exit() में कमांड लाइन पैरामीटर प्रोग्राम प्रवाह में किसी भी बिंदु से वर्तमान प्रक्रिया को समाप्त करने के लिए।

जबकि सफाई से एक मॉड्यूल में अलग हो गया है, यह वास्तव में बिल्ट-इन है और जैसे हमेशा सामान्य परिस्थितियों में उपलब्ध होगा।

वाक्य - विन्यास

  • Sys मॉड्यूल आयात करें और इसे वर्तमान नामस्थान में उपलब्ध कराएँ:

    import sys
    
  • वर्तमान नामस्थान में सीधे sys मॉड्यूल से एक विशिष्ट फ़ंक्शन आयात करें:

    from sys import exit
    

टिप्पणियों

सभी sys मॉड्यूल के सदस्यों के विवरण के लिए, आधिकारिक दस्तावेज देखें।

कमांड लाइन तर्क

if len(sys.argv) != 4:         # The script name needs to be accounted for as well.
    raise RuntimeError("expected 3 command line arguments")

f = open(sys.argv[1], 'rb')    # Use first command line argument.
start_line = int(sys.argv[2])  # All arguments come as strings, so need to be
end_line = int(sys.argv[3])    # converted explicitly if other types are required.

ध्यान दें कि बड़े और अधिक पॉलिश किए गए कार्यक्रमों में आप मॉड्यूल का उपयोग करेंगे जैसे कि कमांड लाइन तर्कों को संभालने के लिए इसे स्वयं करने के बजाय क्लिक करें।

लिपि नाम

# The name of the executed script is at the beginning of the argv list.
print('usage:', sys.argv[0], '<filename> <start> <end>')

# You can use it to generate the path prefix of the executed program
# (as opposed to the current module) to access files relative to that,
# which would be good for assets of a game, for instance.
program_file = sys.argv[0]

import pathlib
program_path = pathlib.Path(program_file).resolve().parent

मानक त्रुटि स्ट्रीम

# Error messages should not go to standard output, if possible.
print('ERROR: We have no cheese at all.', file=sys.stderr)

try:
    f = open('nonexistent-file.xyz', 'rb')
except OSError as e:
    print(e, file=sys.stderr)

समय से पहले प्रक्रिया समाप्त करना और एक निकास कोड वापस करना

def main():
    if len(sys.argv) != 4 or '--help' in sys.argv[1:]:
        print('usage: my_program <arg1> <arg2> <arg3>', file=sys.stderr)
        
        sys.exit(1)    # use an exit code to signal the program was unsuccessful

    process_data()


Modified text is an extract of the original Stack Overflow Documentation
के तहत लाइसेंस प्राप्त है CC BY-SA 3.0
से संबद्ध नहीं है Stack Overflow