473,909 Members | 4,189 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

calling tar commands

12 New Member
I think what I’m trying to do is very basic but I’m a rank beginner and I’m stuck. I’m using Python 2.4.1 on WindowsNT. I’ve installed UnxUtils, which includes the tar commands for Windows.

Immediate objective:
I have a text file containing a list of tar files, one filename per line. For now I’m just trying to loop through my text file and untar each file in the list.

Background/big picture:
Ultimately I’d like to
• Loop through the list and untar each tar file (~1700 files total),
• import some of the un-tarred components of the original tar file into an image processing program (ERDAS Imagine),
• zip the output from Imagine, and
• delete the unneeded files from the original tar.

So far, I’m able to loop through the list and print filenames. Also, the tar command is working from the command line (and batch file) but I’m missing something when I try to integrate the tar command onto python.

Here’s what I’ve got so far:

#####Loop through and print each line (works fine):
Expand|Select|Wrap|Line Numbers
  1.  
  2. import fileinput
  3.  
  4. path = r"C:\WorkSpace\LTC\MRLC_test\NZT\tmp"
  5. inFile = open(path + "\\tarList7.txt", 'r')
  6.  
  7. for line in inFile:
  8.     print line
  9.  
  10. inFile.close()
  11.  
  12. print "Done!"
  13.  
#####Trying to add tar component:
Expand|Select|Wrap|Line Numbers
  1. import fileinput
  2. import tarfile
  3.  
  4. path = r"C:\WorkSpace\LTC\MRLC_test\NZT\tmp"
  5. inFile = open(path + "\\tarList2.txt", "r")
  6.  
  7. line = inFile.readline()
  8. tar = tarfile.open(line)
  9.  
  10. for line in inFile:
  11.     print line
  12.     tar.extractall()
  13.     tar.close()
  14.  
  15. inFile.close()
  16.  
Thanks in advance...
Jun 6 '07 #1
18 3340
ilikepython
844 Recognized Expert Contributor
I think what I’m trying to do is very basic but I’m a rank beginner and I’m stuck. I’m using Python 2.4.1 on WindowsNT. I’ve installed UnxUtils, which includes the tar commands for Windows.

Immediate objective:
I have a text file containing a list of tar files, one filename per line. For now I’m just trying to loop through my text file and untar each file in the list.

Background/big picture:
Ultimately I’d like to
• Loop through the list and untar each tar file (~1700 files total),
• import some of the un-tarred components of the original tar file into an image processing program (ERDAS Imagine),
• zip the output from Imagine, and
• delete the unneeded files from the original tar.

So far, I’m able to loop through the list and print filenames. Also, the tar command is working from the command line (and batch file) but I’m missing something when I try to integrate the tar command onto python.

Here’s what I’ve got so far:

#####Loop through and print each line (works fine):
Expand|Select|Wrap|Line Numbers
  1.  
  2. import fileinput
  3.  
  4. path = r"C:\WorkSpace\LTC\MRLC_test\NZT\tmp"
  5. inFile = open(path + "\\tarList7.txt", 'r')
  6.  
  7. for line in inFile:
  8.     print line
  9.  
  10. inFile.close()
  11.  
  12. print "Done!"
  13.  
#####Trying to add tar component:
Expand|Select|Wrap|Line Numbers
  1. import fileinput
  2. import tarfile
  3.  
  4. path = r"C:\WorkSpace\LTC\MRLC_test\NZT\tmp"
  5. inFile = open(path + "\\tarList2.txt", "r")
  6.  
  7. line = inFile.readline()
  8. tar = tarfile.open(line)
  9.  
  10. for line in inFile:
  11.     print line
  12.     tar.extractall()
  13.     tar.close()
  14.  
  15. inFile.close()
  16.  
Thanks in advance...
What's the error?

I haven't used this module at all but why are you closing the tar file in the for loop?
Jun 6 '07 #2
ghostdog74
511 Recognized Expert Contributor
I think what I’m trying to do is very basic but I’m a rank beginner and I’m stuck. I’m using Python 2.4.1 on WindowsNT. I’ve installed UnxUtils, which includes the tar commands for Windows.

Immediate objective:
I have a text file containing a list of tar files, one filename per line. For now I’m just trying to loop through my text file and untar each file in the list.

Background/big picture:
Ultimately I’d like to
• Loop through the list and untar each tar file (~1700 files total),
• import some of the un-tarred components of the original tar file into an image processing program (ERDAS Imagine),
• zip the output from Imagine, and
• delete the unneeded files from the original tar.

So far, I’m able to loop through the list and print filenames. Also, the tar command is working from the command line (and batch file) but I’m missing something when I try to integrate the tar command onto python.

Here’s what I’ve got so far:

#####Loop through and print each line (works fine):
Expand|Select|Wrap|Line Numbers
  1.  
  2. import fileinput
  3.  
  4. path = r"C:\WorkSpace\LTC\MRLC_test\NZT\tmp"
  5. inFile = open(path + "\\tarList7.txt", 'r')
  6.  
  7. for line in inFile:
  8.     print line
  9.  
  10. inFile.close()
  11.  
  12. print "Done!"
  13.  
#####Trying to add tar component:
Expand|Select|Wrap|Line Numbers
  1. import fileinput
  2. import tarfile
  3.  
  4. path = r"C:\WorkSpace\LTC\MRLC_test\NZT\tmp"
  5. inFile = open(path + "\\tarList2.txt", "r")
  6.  
  7. line = inFile.readline()
  8. tar = tarfile.open(line)
  9.  
  10. for line in inFile:
  11.     print line
  12.     tar.extractall()
  13.     tar.close()
  14.  
  15. inFile.close()
  16.  
Thanks in advance...
extractall() takes in arguments...
Jun 7 '07 #3
ilikepython
844 Recognized Expert Contributor
extractall() takes in arguments...
Yea but they're optional. If a path is not given, it uses the current directory.
Jun 7 '07 #4
ghostdog74
511 Recognized Expert Contributor
Yea but they're optional. If a path is not given, it uses the current directory.
oops, my bad for misreading.
Expand|Select|Wrap|Line Numbers
  1. for line in open("tarlist.txt"):
  2.     line=line.strip()
  3.     tar = tarfile.open(line)
  4.     for t in tar:
  5.         tar.extract(t)
  6.  
Jun 7 '07 #5
tcurdts
12 New Member
Sorry... meant to include the error. I included my code in my original posting to show that I've at least been trying, but I'm so green that I suspect there's a better, cleaner way to do this.

Here's the error msg (trailing newline):

Error Message:
Traceback (most recent call last):
File "C:\Python24\Li b\site-packages\python win\pywin\frame work\scriptutil s.py", line 310, in RunScript
exec codeObject in __main__.__dict __
File "C:\PythonScrip ts\MyScripts\Sc ript3.py", line 9, in ?
tar = tarfile.open(li ne)
File "C:\Python24\li b\tarfile.py", line 916, in open
return func(name, "r", fileobj)
File "C:\Python24\li b\tarfile.py", line 959, in gzopen
fileobj = file(name, mode + "b")
IOError: [Errno 2] No such file or directory: 'NZT05013031111 6199500.tar\n'
Jun 7 '07 #6
ilikepython
844 Recognized Expert Contributor
Sorry... meant to include the error. I included my code in my original posting to show that I've at least been trying, but I'm so green that I suspect there's a better, cleaner way to do this.

Here's the error msg (trailing newline):

Error Message:
Traceback (most recent call last):
File "C:\Python24\Li b\site-packages\python win\pywin\frame work\scriptutil s.py", line 310, in RunScript
exec codeObject in __main__.__dict __
File "C:\PythonScrip ts\MyScripts\Sc ript3.py", line 9, in ?
tar = tarfile.open(li ne)
File "C:\Python24\li b\tarfile.py", line 916, in open
return func(name, "r", fileobj)
File "C:\Python24\li b\tarfile.py", line 959, in gzopen
fileobj = file(name, mode + "b")
IOError: [Errno 2] No such file or directory: 'NZT05013031111 6199500.tar\n'
Try:
Expand|Select|Wrap|Line Numbers
  1. tar = tarfile.open(line[:-1])
  2.  
That will remove the trailing new line.
Jun 7 '07 #7
bartonc
6,596 Recognized Expert Expert
Try:
Expand|Select|Wrap|Line Numbers
  1. tar = tarfile.open(line[:-1])
  2.  
That will remove the trailing new line.
Expand|Select|Wrap|Line Numbers
  1. As will tar = tarfile.open(line.strip())
In case there is no newline at the end.
Jun 7 '07 #8
tcurdts
12 New Member
Thanks much!! I've got the code working (untarring files contained in a txt file) when the python script is located in the same directory as the tar files.

Two more questions:
1) How do I specify different input and output directories so I can run the script from a different location and put my output (untarred files) in yet another directory?
2) Is there redundancy in lines 5 and 8?

Here's the code that's working as described above:
Expand|Select|Wrap|Line Numbers
  1. import fileinput
  2. import tarfile
  3. path = r"C:\WorkSpace\LTC\MRLC_test\NZT\tmp"
  4. tfile = "\\tarList2.txt"
  5. inFile = open(path + tfile)
  6. line = inFile.readline()
  7.  
  8. for line in open(path + tfile):
  9.     line = line.strip()
  10.     print line
  11.     tar = tarfile.open(line)
  12.     for t in tar:
  13.         tar.extract(t)
  14.  
Jun 12 '07 #9
ilikepython
844 Recognized Expert Contributor
Thanks much!! I've got the code working (untarring files contained in a txt file) when the python script is located in the same directory as the tar files.

Two more questions:
1) How do I specify different input and output directories so I can run the script from a different location and put my output (untarred files) in yet another directory?
2) Is there redundancy in lines 5 and 8?

Here's the code that's working as described above:
Expand|Select|Wrap|Line Numbers
  1. import fileinput
  2. import tarfile
  3. path = r"C:\WorkSpace\LTC\MRLC_test\NZT\tmp"
  4. tfile = "\\tarList2.txt"
  5. inFile = open(path + tfile)
  6. line = inFile.readline()
  7.  
  8. for line in open(path + tfile):
  9.     line = line.strip()
  10.     print line
  11.     tar = tarfile.open(line)
  12.     for t in tar:
  13.         tar.extract(t)
  14.  
You don't need to open the file every line. Also it is a good idea to use the os module for files and directories for portability:
Expand|Select|Wrap|Line Numbers
  1. import os, os.path
  2. import tarfile
  3. import fileinput
  4.  
  5. path = raw_input("Enter a directory: ")
  6. tfile = raw_input("Enter the file: ")
  7.  
  8. for line in open(os.path.join(path, tfile)):
  9.     line = line.strip()
  10.     print line
  11.     tar = tarfile.open(line)
  12.     for t in tar:
  13.         tar.extract(t)
  14.  
I replaced your path with raw_input() calls, is that you need?

Also, it might be a good idea to check if the path exists so your program doesn't raise an exception.
Jun 12 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

12
3513
by: Moosebumps | last post by:
So, after reading some messages about os.system, and looking at the popen stuff and trying it a bit, I still have not found a way to keep a command window open for several commands (on Windows 2000/XP), while seeing the normal output in a command window. All I want to do is do what a batch file does, but I want to actually have functions and associative arrays and all the other niceties of python. What's the deal with that? I thought...
11
5054
by: ronan_boisard | last post by:
hi all, I'm trying to call ksh script from python. I have a file (toto.env) with a scirpt looking like: -- begin --- #!/bin/ksh # export TOTO_ENV=/home/toto -- end ---
8
1533
by: Barry Sanderson | last post by:
I'm having a problem calling a .dll file from asp. Here is the situation: I'm creating a web enabled mp3 app that uses windows messaging to control winamp. To this end I've made a dll that controls the basic functions of winamp that works fine out of VB6, but seems to freeze any asp web app that tries to call it. When I create the .DLL file it compiles and registers just fine. In VB6 I use the following code to call it and it seems...
18
12723
by: Simula | last post by:
I am developing an HTML javascript application and I want to preserve state in a way that can be book-marked. I chose HTML anchors as a means of preserving state. When the application changes state, the HTML page URL would change from default.html to default.html#stateA. example: http://pearstudios.com/javascript/locationHashAndFlash.html This has worked perfectly within HTML and javascript alone, however, when connecting Flash and...
1
1177
by: Marina | last post by:
The commands that are generated by the SqlCommandBuilder are not quite right. I am calling GetUpdateCommand and GetDeleteCommand, modifying the results, and assigning them to the UpdateCommand and DeleteCommand properties of the SqlDataAdapter. However, when the update runs, it looks like the original commands that SqlCommandBuilder creates are being used,instead of the commands assigned to the UpdateCommand and DeleteCommand properties....
2
6515
by: Rajat Katyal | last post by:
Hi: I prepare the statement for execution as follows: PREPARE query(text) as SELECT count(*) FROM transform_customer_billing where inv_no = $1; The problem is Iam not able to execute this prepare statement from the stored procedure defined. I added the statement to call the prepare statement is: EXECUTE query('100023'). The ERROR it prints : Function query(text) doesnot exist. Please suggest the solution for this problem.
12
1841
by: creo | last post by:
Hi all! this is a (relatively) newbie question I am writing a shell in Python and I am facing a problem The problem is, after taking the input from user, i have to execute the command which is a python function
15
2837
by: tmp123 | last post by:
Hello, Thanks for your time. We have very big files with python commands (more or less, 500000 commands each file). It is possible to execute them command by command, like if the commands was typed one after the other in a interactive session?
9
2960
by: Ratfish | last post by:
I'm getting a "2014:: Commands out of sync; you can't run this command now" error on a php page when I try to call a second stored procedure against a MySQL db. Does anyone know why I might be getting this error? The error doesn't occur on my development box where I use the 'root' db user, but does occur in production where I'm using a non- root user record to establish a connection. I'm essentially opening a connection at the top of...
0
1260
by: xahlee | last post by:
Here's a little tutorial that lets you write emacs commands for processing the current text selection in emacs in your favorite lang. Elisp Wrapper For Perl Scripts http://xahlee.org/emacs/elisp_perl_wrapper.html plain text version follows. ------------------------------------- Elisp Wrapper For Perl Scripts
0
9877
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11346
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10919
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
11046
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9725
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7248
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4774
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4336
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3357
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.