Skip to main content

Python: Move all files to a single directory without subdirectories

Hi friends,

Below script will move all files in a folder, without subdirectory. Means if you have structure like this :

Then all files in folders Program files, 7-Zip, Lang, CCleaner, Common Files, DESIGNER, logishrd, Microsoft shared, DW EQUATION, 1033 will be moved to a single folder. In this destination, there will be only files and no folders. 

Also, if any file with same name exits already, it will rename the new file as filename_1. If filename_1 also exits, it will rename it as filename_2 and so on. Works with python 3.x.


import os
import os.path
import fileinput
import sys
import shutil
#from fnmatch import fnmatch

#root = '/root/mypython/log'

src1 = 'D:\\doc'
des1 = 'E:\\vbox'

def check_file(self, n):
    name = '.'.join(self.split('.')[:-1])
    ext = self.split('.')[-1]
    if n==1:
        temp=name +'_'+ str(n) + '.' + ext
    if os.path.isfile(temp) == True:
        n= n + 1
        temp = check_file(self,n)
    return temp

full_path = []
full_dir = []
for path, subdirs, files in os.walk(src1):
    for name in files:
        filenames = os.path.join(path, name)

for item in full_path:
    f1 = item.split('\\')[-1]
    des4 = des1 + '\\' + f1
    des5 = check_file(des4,1)
    print(item + ' will be copied to ' + des5 )
    shutil.copyfile(item, des5)



Popular posts from this blog

Python: Rename all files in a folder with names as numbers in ascending order

Hi Friends,

This script will rename files as,, 3.xls etc for all files in same folder as scripts:

import os
x = 0
for filename in os.listdir(os.path.dirname(os.path.abspath(__file__))):
    ext = filename.split('.')
    extension = ext[1]
    os.rename(filename, str(x) + '.' + extension)
    x = x + 1



1. extension = ext[1] collects the extension of file. If you skip this line, the files will be renamed without any extension.

2. Script should be in same folder as files. There should not be any folder in the folder.

Tunneling through putty: Accessing a internal website or service outside the network

Hello friends, there are many situation where you have to open a service or website that is neither in your network nor running with a public IP. The site or service is running properly inside the network, but you have to use it outside the network. Is it simple? Yes. 

First you need to download putty.exe. Go, download and then read further. 

All right, let us assume you are working on a local PC, lets say its IP is  There is another website, which you cannot open on your local PC, as this is on different network. If you can access it directly, because of something you have already done, then no need to read further. Lets say you have another server,,  be it different or same as the website hosting server, but it must be on 192.168.3.x network. Then do as below figures:

You will use the port as per the website. In my experience, even if you use any different port, it will work.
Now, put IP of website in destination, port in source port and then add…

Python: Send mail with attachment ( Any file format)

So you want to send mail with any file as attachment. This script works with any type of attachment. I have given the example of .kml file, an unusual one. You can use pdf, docx etc.

import mimetypes
import email
import email.mime.application
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEImage import MIMEImage
import smtplib

# Create a text/plain message
msg = email.mime.Multipart.MIMEMultipart()
msg['Subject'] = 'Groove'
msg['From'] = ''
msg['To'] = ''

# The main body is just another attachment
body = email.mime.Text.MIMEText("""Hello, how are you? """)

# KML attachment
att = email.mime.application.MIMEApplication(,_subtype="kml")