Today, while working with csv files i got into fantastic situation where i have a list of million values and i make iteration on that. So i thought, it would be easy for me if i split list into number of pieces which dont affect my code, memory and CPU. I can also use generator expression to make make my code run faster, but i was very curious to write code to split list into number of pieces(uses little of generator exp).
And the result is here -
import os,sys
def split_seq(seq, num_pieces):
""" split a list into pieces passed as param """
start = 0
for i in xrange(num_pieces):
stop = start + len(seq[i::num_pieces])
yield seq[start:stop]
start = stop
seq = [i for i in range(100)] ## define your list here
num_of_pieces = 3
for seq in split_seq(seq, num_of_pieces):
print len(seq), '-> ',seq
Showing posts with label listdir. Show all posts
Showing posts with label listdir. Show all posts
Friday, September 10, 2010
Monday, August 23, 2010
Rename multiple file simultaneously
Renaming multiple file once is really little confusing using command-line. There are lots of way to do it via programming, but yet, i didnt fine any on-the-spot command to do it.
So i used python to do it simply.
My requirement was actually:
1) i have one dedicated folder, where i have to rename all files.
2) all filename to be renamed are structured, i mean, i have to rename all dedupe_<number>.csv to <number>.csv
I did this using following code,
import os
from os import listdir, getcwd, rename
list_files = listdir(getcwd())
for filename in list_files:
if not filename.startswith('.') and 'dedupe_' in filename:
ext = filename.split('.')[-1]
new_name = ''.join(filename.split('.')[:-1]).replace('dedupe_','')+'.'+ext
cmd = 'mv '+filename + ' ' +new_name
os.popen(cmd)
isn't it very very simple !!!!
So i used python to do it simply.
My requirement was actually:
1) i have one dedicated folder, where i have to rename all files.
2) all filename to be renamed are structured, i mean, i have to rename all dedupe_<number>.csv to <number>.csv
I did this using following code,
import os
from os import listdir, getcwd, rename
list_files = listdir(getcwd())
for filename in list_files:
if not filename.startswith('.') and 'dedupe_' in filename:
ext = filename.split('.')[-1]
new_name = ''.join(filename.split('.')[:-1]).replace('dedupe_','')+'.'+ext
cmd = 'mv '+filename + ' ' +new_name
os.popen(cmd)
isn't it very very simple !!!!
Subscribe to:
Posts (Atom)