re.split() works in the interpreter, but, fails on script execution. Python 3 - python-3.x

I am having a strange issue. I am trying to write a script to automate NFS mounts and it seems that it is failing on a re.split. I would like to use any number of spaces to delimit the strings, but, for some reason when I run the script it fails. I am generate the following error when I run my script.
basilius#HomeComing:~/PycharmProjects/pythonProject1$ sudo python3 mount_py3.py lin
file.txt rw,noac,suid
Enter the name of the default group: basilius
Enter the default group name: basilius
Traceback (most recent call last):
File "mount_py3.py", line 146, in <module>
main()
File "mount_py3.py", line 125, in main
export, mount_point = re.split(' +', line)
ValueError: not enough values to unpack (expected 2, got 1)
for the following code.
inp_file = open(args.filein, 'r')
for line in inp_file.readline():
export, mount_point = re.split(' +', line)
I use argparse to pass the name of the script, as a string, to the script. It is not being opened by argparse.
When I directly invoke the interpreter it works fine. See below.
basilius#HomeComing:~/PycharmProjects/pythonProject1$ python3
Python 3.6.9 (default, Apr 18 2020, 01:56:04)
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import re
>>> inp_file = open('file.txt', 'r')
>>> line = inp_file.readline()
>>> print(line)
server:/opt/website /srv/program
>>> export, mount_point = re.split(' +', line)
>>> print(export, mount_point)
server:/opt/website /srv/program
>>>
When I do just a straght readlines() on the file it returns everything in the correct format.
It is a straght text file for the export and mount_point for fstab entry. I am not sure why I am getting different results. Could someone assit? I have been pounding the internet for a couple of days now.

The issue is with your loop, where you write for line in inp_file.readline():. This reads a single line from the file, and loops over the characters in the string, assigning each one to line in turn.
You probably want for line in inp_file:, which loops over the lines in the file one at a time. You could also call readlines() (with an "s" on the end) on the file, which does the same thing but uses more memory.
Or, I suppose, if you only care about the first line of the file, you could just do line = inp_file.readline() without the for loop.
Unrelated to your issue, it's a probably good idea to use a with statement to handle the opening and closing of your file: with open(args.filein, 'r') as inp_file:, followed by the rest of the code that uses it indented by one level.

Related

Indentation error while importing python file in Juypter Notebook

I am running simple code mentioned below, but getting indentation error. I am using Python 3.x and Juypter notebook. Help will be appreciated.
import os
import sys
sys.path.insert(0, os.path.abspath('C:\dir python util'))
import h
h.my_first_function()
In file h.py, which is in drive c:\dir python util contents are below:
def my_first_function():
print ("my first program of python")
Error I am getting:
Traceback (most recent call last):
File "C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 3331, in run_code exec(code_obj, self.user_global_ns, self.user_ns)
File "", line 5, in import h
File "C:\dir python util\h.py", line 2 print ("my first program of python") ^ IndentationError: expected an indented block
File "C:\dir python util\h.py", line 2 print ("my first program of python") ^ IndentationError: expected an indented block
Error indicates that it is simple indentation error. You need to maintain the appropriate indentation in the code.
With corrected indentation, it'll be as follows:
def my_first_function():
print ("my first program of python")
So print statement needs to have 4 spaces rather than one.
Suggest you to go through this document to get idea about Python's indentation rules, if you don't know that already.

Iam not quite sure how to define Users in this case

iam a total noob in python; i have a background in chemistry and iam doing my master in computational chemistry. Iam trying to learn computer science as fast as i can.
I currently dont know how to solve this error. I have googled the question but the answers dont actually satisfy.
I would really appreciate if you guys give me hints on how to fix this error.
Thanks,
Thanh Le
In order for the program to work, it uses codes from this file containing:
from RunRMSD import RunRMSD
RunRMSD()
from SumRMSD import SumRMSD
SumRMSD()
then it uses codes from a file (RunRMSD) containing:
run calcRMSD.py to get raw output from pymol
def RunRMSD():
# get output directory from a threefiles.txt
with open('./threefiles.txt') as fi:
fline = fi.readline()
flist = fline.split('\t')
path_output = flist[1]
import os
command = '/opt/local/bin/pymol -cqr '+'./CalcRMSD.py > '/Users/thanhle/Desktop/output/'RMSD.out'
os.system(command)
Not sure if my path is correct though.
thanhs-MacBook-Pro-2:untitled folder thanhle$ python Director_RMSD.py
Traceback (most recent call last):
File "Director_RMSD.py", line 5, in <module>
RunRMSD()
File "/Users/thanhle/Desktop/ftdock-2-dev2/untitled folder/RunRMSD.py", line 11, in RunRMSD
command = '/opt/local/bin/pymol -cqr '+'./CalcRMSD.py > '/Users/thanhle/Desktop/output/'RMSD.out'
NameError: global name 'Users' is not defined
The "command" variable is not well written:
command = '/opt/local/bin/pymol -cqr '+'./CalcRMSD.py > '/Users/thanhle/Desktop/output/'RMSD.out'
The error is thrown because the path /Users/thanhle/Desktop/output/ is not concatenated and also you are missing a apostrophe. If you don't want to parse any variable to the command it should be written:
command = '/opt/local/bin/pymol -cqr ./CalcRMSD.py > /Users/thanhle/Desktop/output/RMSD.out'

Python: NameError from calling a file from the commandline arguments

For an assignment I'm supposed to have to have a line to open a file that is passed as an argument in the commandline, I keep getting
Traceback (most recent call last):
File "execute.py", line 1, in <module>
program=open(programfilename, "r")
NameError: name 'programfilename' is not defined
My code to this point is program=open(programfilename, "r"). I'm not quiet sure what is wrong. It is the first line in my program. Execute.py is the name of my code.
You need to set the programfilename variable to the name/path of the file on a previous line. Alternatively, you could put the filename in quotes instead.
It is the first line in my program
Well there's your problem. You are using programfilename without having defined it first.
Try something like
import sys
programfilename = sys.argv[0] # argument you passed into your program.
program=open(programfilename, "r")
I am not sure what exactly you are trying to.
If you want to call a file using command line, the code can be like this
import sys
with open(sys.argv[1], 'r') as f:
print(f.read())
Run like this
python3 execute.py programfilename
If you want your program to get printed on the console, the code can be like this
import sys
with open(sys.argv[0], 'r') as f:
print(f.read())
This will print the code on the console.
Run like this
python3 execute.py

Python3.4 -Nmap Requires root privileges

Running on Mac Os 10.10.5
Running this script to scan for hosts on the network:
import nmap
nm = nmap.PortScanner()
nm.scan('192.168.5.1/24', arguments='-O')
for h in nm.all_hosts():
if 'mac' in nm[h]['addresses']:
print(nm[h]['addresses'], nm[h]['vendor'])
When running it its printing:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/nmap/nmap.py", line 290, in analyse_nmap_xml_scan
dom = ET.fromstring(self._nmap_last_output)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/xml/etree/ElementTree.py", line 1326, in XML
return parser.close()
File "<string>", line None
xml.etree.ElementTree.ParseError: no element found: line 1, column 0
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/*/Documents/*.py", line 3, in <module>
nm.scan('192.168.0.0/24', arguments='-O')
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/nmap/nmap.py", line 235, in scan
nmap_err_keep_trace = nmap_err_keep_trace)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/nmap/nmap.py", line 293, in analyse_nmap_xml_scan
raise PortScannerError(nmap_err)
nmap.nmap.PortScannerError: 'TCP/IP fingerprinting (for OS scan) requires root privileges.\nQUITTING!\n'
I tried going to that directory and running this command in the terminal:
sudo python *.py
({'mac': '02:62:31:41:6D:84', 'ipv4': '192.168.5.1'}, {})
Any suggestions to run the script from the python IDLE?
Running IDLE as root might work, but it might not be a great idea. sudo idle
Option 1 (recommended):
Put the code requiring elevated privileges in a python file which you run with sudo. I assume you want to play with the results, so you could have the script save the results to a file, which you then read in IDLE.
The following code works in python 2.7 and 3.4
import nmap
import json
nm = nmap.PortScanner()
nm.scan('192.168.5.1/24',arguments='-O') #Note that I tested with -sP to save time
output = []
with open('output.txt', 'a') as outfile:
for h in nm.all_hosts():
if 'mac' in nm[h]['addresses']:
item = nm[h]['addresses']
if nm[h]['vendor'].values():
item['vendor'] = list(nm[h]['vendor'].values())[0]
output.append(item)
json.dump(output, outfile)
Run sudo python nmaproot.py
Since the file is written by root, you need to change ownership back to yourself.
sudo chown -r myusername output.txt
In IDLE:
import json
input = open('output.txt','r'):
json_data = json.load(input)
json_data[0] # first host
Option 2 (not recommended at all):
Use subprocess to run the file with the elevated code as root and return the output. It gets kind of messy and requires you to hardcode your password...but it's possible.
from subprocess import Popen, PIPE
cmd = ['sudo', '-S', 'python', 'nmaproot.py']
sudopass = 'mypassword'
p = Popen(cmd, stdin=PIPE, stderr=PIPE,universal_newlines=True, stdout=PIPE)
output = p.communicate(sudopass + '\n')
I'm unsure of how you can run a given portion of your python code as root without saving it to a file and running it separately. I recommend you go with option 1 as option 2 isn't very good (but it was fun to figure out).
Copy the idle desktop shortcut and name it rootidle then right and change properties. Goto desktop entry and add gksu before /usr/bin/idle3. Then load and run the program
maybe this might help someone here. Found this from one site
scanner.scan(ip_addr, '1-1024', '-v -sS', sudo=True)
use
sudo = True

In Python, list certain type of file in a directory on Linux

In my directory, there are a kind of type of file end in .log file.
In ordinary, I use ls .*log commands to list all files.
However, I wanna to use Python code to handle with it. There are two ways I've tried.
First:
import subprocess
ls_al = subprocess.check_output(['ls','.*log'])
but it returns ls: .*log: No such file or directory
Second:
import subprocess
ls_al = subprocess.check_Popen(['ls','.*log'],stdout=subprocess.PIPE)
ls = ls_al.stdout.read().strip()
but those two didn't work.
Can anyone help with this?
Globbing patterns are expanded by the shell, but you are running the command directly. You'd have to run the command through the shell:
ls_al = subprocess.check_output('ls *.log', shell=True)
where you pass in the full command line to the shell as a string (and use the correct glob syntax).
Demo (using *.py):
>>> subprocess.check_output(['ls', '*.py'])
ls: *.py: No such file or directory
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/mj/Development/Library/buildout.python/parts/opt/lib/python2.7/subprocess.py", line 575, in check_output
raise CalledProcessError(retcode, cmd, output=output)
subprocess.CalledProcessError: Command '['ls', '*.py']' returned non-zero exit status 1
>>> subprocess.check_output('ls *.py', shell=True)
'calc.py\ndAll.py\nexample.py\ninplace.py\nmyTests.py\ntest.py\n'
Note that the correct way in Python is to use os.listdir() with manual filtering, filter with the fnmatch module, or use the glob module to list and filter together:
>>> import glob
>>> glob.glob('*.py')
['calc.py', 'dAll.py', 'example.py', 'inplace.py', 'myTests.py', 'test.py']
.*log seems like regular expression, not globbing pattern. Do you mean *.log? (need shell=True argument to make shell do glob expansion)
BTW, glob.glob('*.log') is more preferable way if you want list of file paths.
Rather than run an external command, you could use Python's os module to get the files in the directory. Then the re module can be used to create a regular expression to filter for your log files. I think this would be a more pythonic approach. It should also work on multiple platforms without modification. Note that in the code below I'm assuming your log files all end with '.log'; if you need something else you'll need to tinker with the regex.
import os
import re
import sys
the_dir = sys.argv[1]
all_files = os.listdir(the_dir)
log_files = []
log_pattern = re.compile('.*\.log')
for fn in all_files:
if re.match(log_pattern, fn):
log_files.append(fn)
print log_files
Why not use glob?
$ ls
abc.txt bar.log def.txt foo.log ghi.txt zoo.log
$ python
>>> import glob
>>> for logfile in glob.glob('*.log'):
... print(logfile)
...
bar.log
foo.log
zoo.log
>>>

Resources