Skip to main content

Posts

Organize you files : Move them to predefined location after prompt for each file

So you have lots of files scattered in directory and you are finding it very difficult to organize. Well my below script will prompt file name of each, and will ask move the file to predefined location. If 3 or invalid selection,  files will not move.

Works only on windows.

==========================================

import os
data1 = [f for f in os.listdir('.') if os.path.isfile(f)]
loc1 = "G:\\BooksLibrary"
loc2 = "G:\\Waste"
for x in data1:
    print "Where do you want to move " + x
    sel1 = raw_input("Press 1 for Book, 2 for waste, 3 for nowhere ")
    if sel1 =="1":
        cmd1 = "move \"" + x + "\" " + loc1
        print cmd1
        os.system(cmd1)
    elif sel1 =="2":
        cmd2 = "move \"" + x + " " +loc2
        print cmd2
        os.system(cmd2)
    elif sel1 =="3":
        print x + " is not moved"
    else:
        print "Invalid selection, f…
Recent posts

Linux : Detach command from Shell

So you have executed a command that is taking time, and you are fearing that if someone closes your shell program ( e.g. putty ), your command will stop?

Not to worry, do as intructed below:

1. Run command ( e.g. tar -xvf myfile.tar )
2. Press ctrl + z  ( This will pause the command )
3. Type "bg"  ( This will send the program to background )
4. Type " disown -h" ( This will  Detach command from Shell )

Now you can close your shell, but your command will remain running.


++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Note : For checking, login on server with another shell, and grep your command:

ps -ef | grep tar

Python Script : Convert txt file to csv file

Dear Friends,

Some times you need to convert txt file to csv file automatically. Below is the code:

import csv

text1 = r"textfile.txt"
csv1 = r"csvfile.csv"

inputtxt = csv.reader(open(text1, "rb"), delimiter = '\t')
outputcsv = csv.writer(open(csv1, 'wb'))

outputcsv.writerows(inputtxt)


++++++++++++++++++++++++++++++++++++++++++

Note : csv package is available in python by default. 




Python Script to add Grub Password Automatically

So you need to add Grub password in 50+ servers? No need to do this manually.


import os, fileinput
pass1 = raw_input('Enter the password for grub ')
cmd1 = 'echo -e "' + pass1 + '\n' + pass1 + '" | /sbin/grub-md5-crypt > log.txt'
os.system(cmd1)

with open('log.txt') as f:
    for line in f:
        cipher = line
insert1 = 'password --md5 ' + cipher

with open ('/etc/grub.conf') as f:
    lines = f.readlines()
    f.close()
lines.insert(lines.index('timeout=5\n') + 1, insert1)

with open('/etc/grub.conf', 'w') as f:
    for l in lines:
        f.write(l)

Python Script : Add text to beginning of a file

Hi,

Adding text to the end of file is staightforward, but it is not so obevious if text need to be added at the beginning of the file.

Below is what can be done:

===========================================

with open('hp.txt','r+') as f:
    old = f.read()
    f.seek(0)
    f.write('Lordooooo\n' + old)
f.close()


===========================================

Notes:

1. f.seek(0) sets the access point to beginning of the file.

2. If you need to add text, say after 5th character. then you need to change if like below:

f.seek(5)




Python Script: Download file over SSH

Script:

import os
import paramiko 
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('10.171.21.99', username="abhishek", password="golden111")
sftp = ssh.open_sftp()
lp = os.path.realpath(__file__)
localpath = 'abc.txt'
remotepath = '/opt/crestelsetup/patchzip/abc.txt'
print localpath
print remotepath
sftp.get(remotepath, localpath)
sftp.close()
ssh.close()


===================================

Notes:

Notes:

1. This downloads 'abc.txt.' from remote server to current directory.

2. This assumes that SSH port is 22, and accessible.

3. Useful if FTP port ( 21 ) is closed. 

4. For uploading file see below link:

http://pc2solution.blogspot.in/2015/04/python-script-upload-file-over-ssh.html

Python Script: Upload file over SSH

Script :

import os
import paramiko 
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('10.171.21.99', username="abhishek", password="golden111")
sftp = ssh.open_sftp()
localpath = 'abc.txt'
remotepath = '/opt/crestelsetup/patchzip/abc.txt'
sftp.put(localpath, remotepath)
sftp.close()
ssh.close()

========================================

Notes:

1. This uploads 'abc.txt.' on the remote server.

2. This assumes that SSH port is 22, and accessible.

3. Useful if FTP port ( 21 ) is closed. 

4. For downloading file, see below link:

http://pc2solution.blogspot.in/2015/04/python-script-download-file-over-ssh.html