LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-30-2013, 09:33 PM   #1
simhumcon
Member
 
Registered: Dec 2013
Posts: 35

Rep: Reputation: Disabled
zipping files with same first 5 character


hello All!! im happy to post my first thread here!

i have been struggling for 6 days to complete this task, but i just cant understand. please help me as im a noobie in linux and python.

i would like to create a python script that will go into a directory, and zip all the filenames that has first 5 same characters. all the filenames that has first 5 same characters will be zipped together. can anyone help me please??
 
Old 12-30-2013, 09:50 PM   #2
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,131
Blog Entries: 2

Rep: Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833
Break the problem down into smaller problems that you can solve easier:
1. Reading the filenames
2. Sort the list
3. Add the first file to a zip archive
4. Compare the first characters of the next name in the list with the current one
5. If they have the same characters add the next name to the zip-file, otherwise create a new one
6. Go back to 4 until you iterated over the whole list
 
Old 12-31-2013, 12:34 AM   #3
simhumcon
Member
 
Registered: Dec 2013
Posts: 35

Original Poster
Rep: Reputation: Disabled
zipping files with same first 5 character

hi TobiSGD,

thank you very much for the reply.
the directory will have many files, and some of the filenames will have same first 5 character.

im having problem with "sort the list". the filenames will keep changing and i cant hardcode the first 5 characters of the filenames. i need to find a way where i can detect any files that has same first 5 character and zip them up. something like "filename[:5]"
 
Old 01-02-2014, 07:48 AM   #4
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,131
Blog Entries: 2

Rep: Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833
If you look at the links I posted you will find that the function that reads the filenames in a directory will return a list of filenames. If you sort that list filenames that begin with the same characters will always be immediate neighbors in that list, so you will only have to compare with the next member of the list. You can indeed do that with using the filename[:5] notation.
 
Old 01-02-2014, 03:02 PM   #5
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 3,244

Rep: Reputation: 1403Reputation: 1403Reputation: 1403Reputation: 1403Reputation: 1403Reputation: 1403Reputation: 1403Reputation: 1403Reputation: 1403Reputation: 1403
Why bother with the sort? Take each file name in turn and add that file to a ZIP file with a name based on the first 5 characters of that name. When you are done, each ZIP file will contain file names starting with the same 5 characters.
 
Old 01-02-2014, 03:07 PM   #6
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,131
Blog Entries: 2

Rep: Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833
Quote:
Originally Posted by rknichols View Post
Why bother with the sort? Take each file name in turn and add that file to a ZIP file with a name based on the first 5 characters of that name. When you are done, each ZIP file will contain file names starting with the same 5 characters.
Indeed, this approach looks much better than mine.
 
Old 01-07-2014, 01:03 AM   #7
simhumcon
Member
 
Registered: Dec 2013
Posts: 35

Original Poster
Rep: Reputation: Disabled
hi rknichols and TobiSGD,

thank you for all your replies! the thing is, i'm really new in coding. can please please give me a clue on how to create such a zipping function?

Thanks,
Barathan R
 
Old 01-07-2014, 03:11 AM   #8
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,131
Blog Entries: 2

Rep: Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833
RootOfTheNull has a nice series of video-tutorials about this, starting with this video: http://youtu.be/zykIxD5PciE
 
Old 01-07-2014, 05:48 AM   #9
simhumcon
Member
 
Registered: Dec 2013
Posts: 35

Original Poster
Rep: Reputation: Disabled
hello,

im sorry, the video shows how to add files into zip, but is not telling me how to add same filenames. the main problem im having here is i cant figure out how to just write files with same first 5 character into a zipfile
 
Old 01-07-2014, 06:25 AM   #10
simhumcon
Member
 
Registered: Dec 2013
Posts: 35

Original Poster
Rep: Reputation: Disabled
so, currently, here is my script. but i dont know how to put into programming words that "only zip files with same name together"
please help me out with this

#!/usr/bin/env python

import zipfile
import os,glob

path = '/usr/local/testlogs/test/'

def zipfunc(path, myzip):
for path,dirs, files in os.walk(path):
for file in files:
if os.path.isfile(os.path.join(path,file)):
myzip.write(os.path.join(os.path.basename(path), file))


if __name__ == '__main__':
path= '/usr/local/testlogs/test/'
myzip = zipfile.ZipFile('myzipped.zip', 'w')
zipfunc(path,myzip)
myzip.close()
 
Old 01-07-2014, 09:51 AM   #11
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,131
Blog Entries: 2

Rep: Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833
Sorry, but if you post Python code you need to use code-tags, so that formatting is preserved. Indentation is crucial for understanding Python code, without it is useless. For more info about code-tags see my signature.
 
Old 01-07-2014, 10:16 AM   #12
simhumcon
Member
 
Registered: Dec 2013
Posts: 35

Original Poster
Rep: Reputation: Disabled
Code:
#!/usr/bin/env python

import zipfile
import os,glob

path = '/usr/local/testlogs/test/'

def zipfunc(path, myzip):
    for path,dirs, files in os.walk(path):
            for file in files:
                if  os.path.isfile(os.path.join(path,file)):
                    myzip.write(os.path.join(os.path.basename(path), file))


if __name__ == '__main__':
    path= '/usr/local/testlogs/test/'
    myzip = zipfile.ZipFile('myzipped.zip', 'w')
    zipfunc(path,myzip)
    myzip.close()
 
Old 01-07-2014, 10:33 AM   #13
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 3,244

Rep: Reputation: 1403Reputation: 1403Reputation: 1403Reputation: 1403Reputation: 1403Reputation: 1403Reputation: 1403Reputation: 1403Reputation: 1403Reputation: 1403
You need to extract the first 5 characters of the file name and save that in a variable. Then use that variable as part of the name of the ZIP file (i.e., not putting everything into "myzipped.zip").

Sorry, but I'm not a Python programmer so I can't be more specific. (Every time I start working my way through the Dive Into Python book , I end up as a lump in the belly of a snake. )
 
  


Reply

Tags
python


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
some files are missing while zipping prabhakaran0608 Linux - Newbie 1 02-28-2012 07:37 AM
Zipping multiple files Quads Linux - Newbie 8 12-29-2011 12:47 PM
Zipping of files rajesh_b Programming 5 10-27-2004 06:02 AM
zipping files with a script quickk Linux - Newbie 8 09-17-2004 09:32 AM
zipping/compressing folders and files DiZASTiX Linux - Newbie 1 05-26-2003 08:48 PM


All times are GMT -5. The time now is 11:05 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration