tags:

views:

93

answers:

3

hi ,

I am new to Python and need some help.

i need to write a script that will look for file in c:\script\test\ directory with ext ".dat" and find "^" in there and replace with "|"

i am not sure how to write this. There will only be one file for a day in the directory with the current date as the file name.

Please help. I am not a good programmer obviously

thanks

A: 

Start here: http://diveintopython.org/toc/index.html You'll be interested in endswith, open, and replace. splitext could be good if you're extra-careful.

Chris
A: 

Example: Check if file exists or not?

import os.path
# os.path - The key to File I/O
os.path.exists("foo.txt")

To learn more details : os.path

Resources to learn more:

Use this excellent resource if you want to learn the basics in organized way :Google's Python Class - should clear your doubts about how to do string operations.

@Chris's answer has a very good link in it, so use that one to learn more.

note: Anyone here can give you exact code but you won't learn that way.

Gollum
A: 
import glob
for filename in glob.glob(r"C:\script\test\*.dat"):
    with open(filename, 'rb') as inputfile:
        data = inputfile.read()
    with open(filename, 'wb') as outputfile:
        outputfile.write(data.replace("^", "|"))

should work in Python 2.6 and 2.7. Make sure you make a backup of your file first.

Tim Pietzcker