Hi there,
I'm very new to python, the problem I need to solve is whats the "best/
simplest/cleanest" way to read in multiple files (ascii), do stuff to
them, and write them out(ascii).
--
import os
filePath = ('O:/spam/eggs/')
for file in os.listdir(file Path): #straight from docs
# iterate the function through all the files in the directory
# write results to separate files <- this is where I'm mostly
stuck.
--
For clarity's sake, the file naming conventions for the files I'm
reading from are file.1.txt -file.nth.txt
It's been a long day, i'm at my wits end, so I apologize in advance if
I'm not making much sense here.
syntax would also be great if you can share some recipes.
Thanks in advance.
h. 8 1582
On 16 Mar, 03:56, "hiro" <Nun...@gmail.c omwrote:
Hi there,
I'm very new to python, the problem I need to solve is whats the "best/
simplest/cleanest" way to read in multiple files (ascii), do stuff to
them, and write them out(ascii).
--
import os
filePath = ('O:/spam/eggs/')
for file in os.listdir(file Path): #straight from docs
# iterate the function through all the files in the directory
# write results to separate files <- this is where I'm mostly
stuck.
--
For clarity's sake, the file naming conventions for the files I'm
reading from are file.1.txt -file.nth.txt
It's been a long day, i'm at my wits end, so I apologize in advance if
I'm not making much sense here.
syntax would also be great if you can share some recipes.
I'd try the glob module. -
import glob
-
-
# Get a list of filenames matching wildcard criteria
-
# (note that path is relative to working directory of program)
-
matching_file_list = glob.glob('O:/spam/eggs/*.txt')
-
-
# For each file that matches, open it and process it in some way...
-
for filename in matching_file_list:
-
infile = file(filename)
-
outfile = file(filename + '.out','w')
-
# Process the input file line by line...
-
for line in infile:
-
pass # Do something more useful here, change line and write to
-
outfile?
-
# Be explicit with file closures
-
outfile.close()
-
infile.close()
-
Of course, you can change the wild card criteria in the glob
statement, and also then filter further using regular expressions to
choose only files matching more specific criteria. This should be
enough to get you started though.
hth
Jon.
On 16 Mar, 09:02, "Jon Clements" <jon...@googlem ail.comwrote:
On 16 Mar, 03:56, "hiro" <Nun...@gmail.c omwrote:
Hi there,
I'm very new to python, the problem I need to solve is whats the "best/
simplest/cleanest" way to read in multiple files (ascii), do stuff to
them, and write them out(ascii).
--
import os
filePath = ('O:/spam/eggs/')
for file in os.listdir(file Path): #straight from docs
# iterate the function through all the files in the directory
# write results to separate files <- this is where I'm mostly
stuck.
--
For clarity's sake, the file naming conventions for the files I'm
reading from are file.1.txt -file.nth.txt
It's been a long day, i'm at my wits end, so I apologize in advance if
I'm not making much sense here.
syntax would also be great if you can share some recipes.
I'd try the glob module.
-
import glob
-
# Get a list of filenames matching wildcard criteria
-
# (note that path is relative to working directory of program)
-
matching_file_list = glob.glob('O:/spam/eggs/*.txt')
-
# For each file that matches, open it and process it in some way...
-
for filename in matching_file_list:
-
infile = file(filename)
-
outfile = file(filename + '.out','w')
-
# Process the input file line by line...
-
for line in infile:
-
pass # Do something more useful here, change line and write to
-
outfile?
-
# Be explicit with file closures
-
outfile.close()
-
infile.close()
-
Of course, you can change the wild card criteria in the glob
statement, and also then filter further using regular expressions to
choose only files matching more specific criteria. This should be
enough to get you started though.
hth
Jon.- Hide quoted text -
- Show quoted text -
Okies; postcoding before finishing your early morning coffee is not
the greatest of ideas!
I forgot to mention that glob will return pathnames as well. You'll
need to check that os.path.isfile( filename) returns True before
processing it...
Jon.
Maybe the walk method in os module is what you need http://docs.python.org/lib/os-file-dir.html
Regards
Jon Clements wrote:
On 16 Mar, 09:02, "Jon Clements" <jon...@googlem ail.comwrote:
>On 16 Mar, 03:56, "hiro" <Nun...@gmail.c omwrote:
Hi there,
I'm very new to python, the problem I need to solve is whats the "best/
simplest/cleanest" way to read in multiple files (ascii), do stuff to
them, and write them out(ascii).
--
import os
filePath = ('O:/spam/eggs/')
for file in os.listdir(file Path): #straight from docs
# iterate the function through all the files in the directory
# write results to separate files <- this is where I'm mostly
stuck.
--
For clarity's sake, the file naming conventions for the files I'm
reading from are file.1.txt -file.nth.txt
It's been a long day, i'm at my wits end, so I apologize in advance if
I'm not making much sense here.
syntax would also be great if you can share some recipes.
I'd try the glob module.
- import glob
- # Get a list of filenames matching wildcard criteria
- # (note that path is relative to working directory of program)
- matching_file_list = glob.glob('O:/spam/eggs/*.txt')
- # For each file that matches, open it and process it in some way...
- for filename in matching_file_list:
- infile = file(filename)
- outfile = file(filename + '.out','w')
- # Process the input file line by line...
- for line in infile:
- pass # Do something more useful here, change line and write to
- outfile?
- # Be explicit with file closures
- outfile.close()
- infile.close()
Of course, you can change the wild card criteria in the glob statement, and also then filter further using regular expressions to choose only files matching more specific criteria. This should be enough to get you started though.
hth
Jon.- Hide quoted text -
- Show quoted text -
Okies; postcoding before finishing your early morning coffee is not
the greatest of ideas!
I forgot to mention that glob will return pathnames as well. You'll
need to check that os.path.isfile( filename) returns True before
processing it...
Jon.
On Mar 16, 7:09 am, Laurent Rahuel <lrahuel.notg.. .@voila.frwrote :
Maybe the walk method in os module is what you needhttp://docs.python.org/lib/os-file-dir.html
Regards
Jon Clements wrote:
On 16 Mar, 09:02, "Jon Clements" <jon...@googlem ail.comwrote:
On 16 Mar, 03:56, "hiro" <Nun...@gmail.c omwrote:
Hi there,
I'm very new to python, the problem I need to solve is whats the "best/
simplest/cleanest" way to read in multiple files (ascii), do stuff to
them, and write them out(ascii).
--
import os
filePath = ('O:/spam/eggs/')
for file in os.listdir(file Path): #straight from docs
# iterate the function through all the files in the directory
# write results to separate files <- this is where I'm mostly
stuck.
--
For clarity's sake, the file naming conventions for the files I'm
reading from are file.1.txt -file.nth.txt
It's been a long day, i'm at my wits end, so I apologize in advance if
I'm not making much sense here.
syntax would also be great if you can share some recipes.
I'd try the glob module.
-
-
-
# Get a list of filenames matching wildcard criteria
-
# (note that path is relative to working directory of program)
-
matching_file_list = glob.glob('O:/spam/eggs/*.txt')
-
-
-
-
-
-
-
# For each file that matches, open it and process it in some way...
-
for filename in matching_file_list:
-
infile = file(filename)
-
outfile = file(filename + '.out','w')
-
# Process the input file line by line...
-
for line in infile:
-
pass # Do something more useful here, change line and write to
-
outfile?
-
# Be explicit with file closures
-
outfile.close()
-
infile.close()
-
-
-
-
Of course, you can change the wild card criteria in the glob
statement, and also then filter further using regular expressions to
choose only files matching more specific criteria. This should be
enough to get you started though.
hth
Jon.- Hide quoted text -
- Show quoted text -
Okies; postcoding before finishing your early morning coffee is not
the greatest of ideas!
I forgot to mention that glob will return pathnames as well. You'll
need to check that os.path.isfile( filename) returns True before
processing it...
Jon.
Also, leaving the format as .out is not necessarily convenient. You
had glob do a search for .txt, so how about doing:
Also, Python advises using open() over file() (although I admit to
using file() myself more often than not)
>>for filename in matching_file_l ist: infile = open(filename,' r') # add 'r' for clarity if nothing else outfile = open(filename[:-4] + '.out.txt','w') # assumes file ext of original file is .txt # Process the input file line by line... for line in infile: pass # do thing --you don't have to iterate line by line, if you specified what you wanted to do to each file we could probably help out here if you need it. # Be explicit with file closures outfile.close() infile.close()
Might also add some try/except statements to be safe ;).
Cheers,
Jordan
The general idiom for altering lines in a file is to open the original
file and write the alterations to a temp file. After you are done
writing to the temp file, delete the original file, and change the
temp file name to the original file name.
If instead you were to read the whole file into a variable, and then
start overwriting the original file with the altered data, if your
program should happen to crash after writing one line to the file, all
the data in your variable would disappear into the ether, and your
file would only contain one line of data. You can imagine what that
would be like if you had lots of important data in the file.
Here's my attempt that incorporates a temp file:
-----
import os
filepath = "./change_files"
li = os.listdir(file path)
for name in li:
fullpath = filepath + "/" + name
if os.path.isfile( fullpath):
infile = open(fullpath, 'r')
lastDotPos = fullpath.rfind( ".")
fileName = fullpath[:lastDotPos]
ext = fullpath[lastDotPos:]
tempName = fileName + ext + ".temp"
outfile = open(tempName, "w")
for line in infile:
outfile.write(l ine + "altered\n" )
outfile.close()
os.remove(fullp ath)
os.rename(tempN ame, tempName[:-5])
---------------
I think it also needs some kind of check to make sure you have
permissions to delete the original file.
On Mar 16, 9:38 am, "7stud" <bbxx789_0...@y ahoo.comwrote:
-----
import os
filepath = "./change_files"
li = os.listdir(file path)
for name in li:
fullpath = filepath + "/" + name
if os.path.isfile( fullpath):
infile = open(fullpath, 'r')
lastDotPos = fullpath.rfind( ".")
fileName = fullpath[:lastDotPos]
ext = fullpath[lastDotPos:]
tempName = fileName + ext + ".temp"
outfile = open(tempName, "w")
for line in infile:
outfile.write(l ine + "altered\n" )
outfile.close()
os.remove(fullp ath)
os.rename(tempN ame, tempName[:-5])
---------------
I did some unnecessary name manipulation in there. Try this:
filepath = "./change_files"
li = os.listdir(file path)
for name in li:
fullpath = filepath + "/" + name
if os.path.isfile( fullpath):
infile = open(fullpath, 'r')
tempName = fullpath + ".temp"
outfile = open(tempName, "w")
for line in infile:
outfile.write(l ine + "altered\n" )
outfile.close()
os.remove(fullp ath)
os.rename(tempN ame, tempName[:-5])
Thanks a lot for the help guys, I'm at work right now and I will go
over your suggestions one by one this weekend. Being more alert now,
taking a look at the examples you posted, I now see how to approach
this problem. The thing with python that I'm starting to realize is
that there are a million different ways to approach a problem, so I
find it great for experimenting (when time allows) yet very
challenging to choose an approach.
Cheers,
h.
Thanks a lot for the help guys, I'm at work right now and I will go
over your suggestions one by one this weekend. Being more alert now,
taking a look at the examples you posted, I now see how to approach
this problem. The thing with python that I'm starting to realize is
that there are a million different ways to approach a problem, so I
find it great for experimenting (when time allows) yet very
challenging to choose an approach.
Cheers,
h. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Andrew Dalke |
last post by:
Is there an author index for the new version of the
Python cookbook? As a contributor I got my comp version
delivered today and my ego wanted some gratification.
I couldn't find my entries.
Andrew
dalke@dalkescientific.com
|
by: Dieter Vanderelst |
last post by:
Dear all,
I'm currently comparing Python versus Perl to use in a project that
involved a lot of text processing. I'm trying to determine what the most
efficient language would be for our purposes. I have to admit that,
although I'm very familiar with Python, I'm complete Perl noob (and I
hope to stay one) which is reflected in my questions.
I know that the web offers a lot of resources on Python/Perl
differences. But I couldn't find a...
|
by: Lilavivat |
last post by:
Running SUSE 10.1 on an AMD64. When I try and run a python program I get
the following error:
/usr/bin/python2: bad interpreter: No such file or directory
"which python" gives me "/usr/local/bin/python"
"which python2.4" gives me "/usr/local/bin/python2.4"
But /usr/bin/python is symlinked to python2.4 "python -python2.4"
|
by: Juha S. |
last post by:
Hi,
I'm writing a small text editor type application with Python 2.5 and
Tkinter. I'm using the Tk text widget for input and output, and the
problem is that when I try to save its contents to a .txt file, any
Scandinavian letters such as "äöå ÄÖÅ" are saved incorrectly and show up
as a mess when I open the .txt file in Windows Notepad.
It seems that the characters will only get mixed if the user has typed
them into the widget, but if...
|
by: Derek Martin |
last post by:
I'd like to know if it's possible to code something in Python which
would be equivalent to the following C:
---- debug.c ----
#include <stdio.h>
bool DEBUG;
| |
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: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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...
| | |