Skip to content Skip to sidebar Skip to footer

Python Packages.'import Sys' Vs 'from Sys Import Argv'

I'm trying to use 'argv' into my python script. For this i'm using following code: from sys import argv script, file_name = argv print(file_name) python3 e.py e.txt This code work

Solution 1:

Import sys does not give you direct access to any of the names inside sys itself To access those you need to prefix them with sys:

sys.argv
sys.exit()

If you want use argv instead sys.argv try

from sys import *

Solution 2:

@Aman, have a look at the following 2 code examples.

Check this beautiful https://www.python.org/dev/peps/pep-0008/?#imports for more information about imports.

sys_file_name.py »

import sys

import sys

script, file_name = sys.argv;

print(script, file_name);

Output »

E:\Users\Rishikesh\Python3\Practice\SysFileName>python sys_file_name.py  file1.txt
sys_file_name.py file1.txt

sys_file_name2.py »

from sys import argv

from sys import argv

script, file_name = argv;

print(script, file_name);

Output »

E:\Users\Rishikesh\Python3\Practice\SysFileName>python sys_file_name2.py file2.txt
sys_file_name2.py file2.txt

Post a Comment for "Python Packages.'import Sys' Vs 'from Sys Import Argv'"