Categories
Uncategorized

Input from file, then move files – Python

This bit of code will take the input from a file, create a series of files with matching directories (folders), then move the file into the directory (easier than changing the directory)

The input file reads…

# input.txt
1234235
1234754
1345787
98745
945687

This is the Python code…

#!/usr/bin/python

# Import all the things we need

from os import makedirs
from re import sub
from shutil import move

# this is the input file
mainInputFile="input.txt"

# read the input file
rTheInput=open(mainInputFile,"r")

# for each line (number) in the file...
for line in rTheInput:
    # remove the "new line"
	threadID=sub("\n","",line)
    # make the new file with the number as its name
	theFile=threadID+".txt"
	wFile=open(theFile,"w")
	wFile.close()
    # make a folder with the number as the name
	makedirs(threadID)
    # move the file to the folder
	move(theFile,threadID)

So you should end up with something like this

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s