Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8125567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:51:49+00:00 2026-06-06T06:51:49+00:00

I have the following python script which takes some inputs and puts them in

  • 0

I have the following python script which takes some inputs and puts them in files depending on the sys.argv. I want to add in some duplicate entry checking… As in if a value is passed through sys.argv to be put into a file but it already exists do nothing, else print line to the file.

I was thinking of doing this with subprocess and using the systems find/grep commands (for windows/linux respectively), however I cannot get this test to work.

Any thoughts/ code welcome please.

Thanks

# Import Modules for script
import os, sys, fileinput, platform, subprocess

# Global variables
hostsFile = "hosts.txt"
hostsLookFile = "hosts.csv"
hostsURLFileLoc = "urls.conf"

# Determine platform
plat = platform.system()

if plat == "Windows":

# Define Variables based on Windows and process
    #currentDir = os.getcwd()
    currentDir = "C:\\Program Files\\Splunk\\etc\\apps\\foo\\bin"
    hostsFileLoc = currentDir + "\\" + hostsFile
    hostsLookFileLoc = currentDir + "\\..\\lookups\\" + hostsLookFile
    hostsURLFileLoc = currentDir + "\\..\\default\\" + hostsURLFileLoc
    hostIP = sys.argv[1]
    hostName = sys.argv[2]
    hostURL = sys.argv[3]
    hostMan = sys.argv[4]
    hostModel = sys.argv[5]
        hostDC = sys.argv[6]



    # Add ipAddress to the hosts file for python to process 
    with open(hostsFileLoc,'a') as hostsFilePython:

#       print "Adding ipAddress: " + hostIP + " to file for ping testing"
#       print "Adding details: " + hostIP + "," + hostName + "," + hostURL + "," + hostMan + "," + hostModel + " to file"
        hostsFilePython.write(hostIP + "\n")

    # Add all details to the lookup file for displaying on-screen and added value
    with open(hostsLookFileLoc,'a') as hostsLookFileCSV:

        hostsLookFileCSV.write(hostIP + "," + hostName + "," + hostURL + "," + hostMan + "," + hostModel + "," + hostDC +"\n")

    if hostURL != "*":  

        with open(hostsURLFileLoc,'a+') as hostsURLPython:

            hostsURLPython.write("[" + hostName + "]\n" + "ping_url = " + hostURL + "\n") 

UPDATE: I am trying a small snippet of called based on that provided by steveha, I’m having trouble with the os.rename part

>>> import os
>>> import sys
>>> in_file = "inFile.txt"
>>> out_file = "outFile.txt"
>>> dir = "C:\\Python27\\"
>>> found_in_file = False
>>> with open(in_file) as in_f, open(out_file,"w") as out_f:
...     for line in in_f:
...             if line.endswith("dax"):
...                     found_in_file = True
...     if not found_in_file:
...             out_f.write("192.168.0.199\tdax\n")
...     os.rename( os.path.join(dir, in_f), os.path.join(dir,out_f))

to which I get the following error.

Traceback (most recent call last):
  File "<stdin>", line 7, in <module>
  File "C:\Python27\lib\ntpath.py", line 73, in join
    elif isabs(b):
  File "C:\Python27\lib\ntpath.py", line 57, in isabs
    s = splitdrive(s)[1]
  File "C:\Python27\lib\ntpath.py", line 125, in splitdrive
    if p[1:2] == ':':
TypeError: 'file' object is not subscriptable

any thoughts?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T06:51:51+00:00Added an answer on June 6, 2026 at 6:51 am

    It would be easier, and faster, to do the “grep” tasks in Python directly:

    with open("filename") as f:
        for line in f:
            if "foo" in line:
               ... # do something to handle the case of "foo" in line
    

    Here is a program that will add a host called “dax” to /etc/hosts:

    import sys
    _, in_fname, out_fname = sys.argv
    
    found_in_file = False
    with open(in_fname) as in_f, open(out_fname, "w") as out_f:
        for line in lst:
            if line.endswith("dax"):
                found_in_file = True
            out_f.write(line)
        if not found_in_file:
            out_f.write("192.168.0.199\tdax\n")
    

    You specify two filenames, and the output filename will get a copy of /etc/hosts. If the system name “dax” is already found in /etc/hosts, the copy will be exact; otherwise a line will be appended.

    You can extend this idea by using a regular expression to detect a particular line, and you can edit a line by writing a different line instead of the original.

    This program uses a regular expression to find all entries in the /etc/hosts file that are in the range from 192.168.0.10 to 192.168.0.59 inclusive. Those lines are rewritten to move them to 192.168.1.* where the * is the original address, unchanged.

    import re
    import sys
    _, in_fname, out_fname = sys.argv
    
    pat = re.compile(r'^192.168.0.(\d+)\s+(\S+)')
    
    with open(in_fname) as in_f, open(out_fname, "w") as out_f:
        for line in in_f:
            m = pat.search(line)
            if m:
                x = int(m.group(1))
                if 10 <= x < 60:
                    line = "192.168.1." + str(x) + "\t" + m.group(2) + "\n"
            out_f.write(line)
    

    When you have successfully written the output file, and there was no error, you can use os.rename() to rename the new file to the original filename, thus overwriting the old file. If you figure out that you didn’t need to change any lines in the old file, you can just delete the new file instead of renaming it; if you always rename, you will be updating the modification time stamp on your file even though you didn’t actually change anything.

    EDIT: here is an example of running the last code sample. Say you put the code into a file called move_subnet.py, then on Linux or other *NIX you can run it like this:

    python move_subnet.py /etc/hosts ./hosts_copy.txt
    

    If you are using Windows, it will be something like this:

    python move_subnet.py C:/Windows/system32/drivers/etc/hosts ./hosts_copy.txt
    

    Note that Windows can use backslashes or forward slashes in filenames. I used forward slashes in this example.

    Your output file will be hosts_copy.txt in the current directory.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written following script in python which works fine: from sys import argv
I have the following in my python script.. hostIP = sys.argv[1] hostsFileLoc = hosts.txt
I have the following asp script which uses python 2.5: <%@ Language = Python
I have the following simple python test script that uses Suds to call a
i have something similar to the following... used a python script to JSON to
I have the following script for sending mails using python import smtplib from email.mime.multipart
I'm trying to solve the following problem: Say I have a Python script (let's
I have a python script that I want to debug with python-mode . I
Currently, I have a script which does the following. If I have text file
I have a python script which extracts unique IP addresses from a log file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.