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): -
-
import fileinput
-
-
path = r"C:\WorkSpace\LTC\MRLC_test\NZT\tmp"
-
inFile = open(path + "\\tarList7.txt", 'r')
-
-
for line in inFile:
-
print line
-
-
inFile.close()
-
-
print "Done!"
-
#####Trying to add tar component: -
import fileinput
-
import tarfile
-
-
path = r"C:\WorkSpace\LTC\MRLC_test\NZT\tmp"
-
inFile = open(path + "\\tarList2.txt", "r")
-
-
line = inFile.readline()
-
tar = tarfile.open(line)
-
-
for line in inFile:
-
print line
-
tar.extractall()
-
tar.close()
-
-
inFile.close()
-
Thanks in advance...
18 3340
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): -
-
import fileinput
-
-
path = r"C:\WorkSpace\LTC\MRLC_test\NZT\tmp"
-
inFile = open(path + "\\tarList7.txt", 'r')
-
-
for line in inFile:
-
print line
-
-
inFile.close()
-
-
print "Done!"
-
#####Trying to add tar component: -
import fileinput
-
import tarfile
-
-
path = r"C:\WorkSpace\LTC\MRLC_test\NZT\tmp"
-
inFile = open(path + "\\tarList2.txt", "r")
-
-
line = inFile.readline()
-
tar = tarfile.open(line)
-
-
for line in inFile:
-
print line
-
tar.extractall()
-
tar.close()
-
-
inFile.close()
-
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?
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): -
-
import fileinput
-
-
path = r"C:\WorkSpace\LTC\MRLC_test\NZT\tmp"
-
inFile = open(path + "\\tarList7.txt", 'r')
-
-
for line in inFile:
-
print line
-
-
inFile.close()
-
-
print "Done!"
-
#####Trying to add tar component: -
import fileinput
-
import tarfile
-
-
path = r"C:\WorkSpace\LTC\MRLC_test\NZT\tmp"
-
inFile = open(path + "\\tarList2.txt", "r")
-
-
line = inFile.readline()
-
tar = tarfile.open(line)
-
-
for line in inFile:
-
print line
-
tar.extractall()
-
tar.close()
-
-
inFile.close()
-
Thanks in advance...
extractall() takes in arguments...
extractall() takes in arguments...
Yea but they're optional. If a path is not given, it uses the current directory.
Yea but they're optional. If a path is not given, it uses the current directory.
oops, my bad for misreading. -
for line in open("tarlist.txt"):
-
line=line.strip()
-
tar = tarfile.open(line)
-
for t in tar:
-
tar.extract(t)
-
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'
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: -
tar = tarfile.open(line[:-1])
-
That will remove the trailing new line.
bartonc 6,596
Recognized Expert Expert
Try: -
tar = tarfile.open(line[:-1])
-
That will remove the trailing new line.
- As will tar = tarfile.open(line.strip())
In case there is no newline at the end.
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: -
import fileinput
-
import tarfile
-
path = r"C:\WorkSpace\LTC\MRLC_test\NZT\tmp"
-
tfile = "\\tarList2.txt"
-
inFile = open(path + tfile)
-
line = inFile.readline()
-
-
for line in open(path + tfile):
-
line = line.strip()
-
print line
-
tar = tarfile.open(line)
-
for t in tar:
-
tar.extract(t)
-
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: -
import fileinput
-
import tarfile
-
path = r"C:\WorkSpace\LTC\MRLC_test\NZT\tmp"
-
tfile = "\\tarList2.txt"
-
inFile = open(path + tfile)
-
line = inFile.readline()
-
-
for line in open(path + tfile):
-
line = line.strip()
-
print line
-
tar = tarfile.open(line)
-
for t in tar:
-
tar.extract(t)
-
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: -
import os, os.path
-
import tarfile
-
import fileinput
-
-
path = raw_input("Enter a directory: ")
-
tfile = raw_input("Enter the file: ")
-
-
for line in open(os.path.join(path, tfile)):
-
line = line.strip()
-
print line
-
tar = tarfile.open(line)
-
for t in tar:
-
tar.extract(t)
-
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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 ---
|
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...
|
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...
|
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....
| |
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.
|
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
|
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?
|
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...
|
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
|
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,...
| |
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...
|
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...
|
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,...
|
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...
|
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();...
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |