Hi,I was wondering how to read and write strings in python. I have this code where I'm creating a path, but its a string, and now I want to write to this string. But keep getting this error message AttributeError: 'str' object has no attribute 'write'
I was thinking about using StringIO(), but I'm not sure how it's used.

create a path1
create a newfile

for each filename in path1:  # only look at the filenames of all the txtfiles in path1
    create fullpath1 + filename            #create fullpath
    create newfile + filenames from path1  #create fullpath 
    open fullpath1
    for each line in fullpath1:
        get some information from fullpath1
        write it to newfile

path1 is just the path to a lot of txt files, and in these txt files I want to get some information and create new txt files, but with the same filenames as the other txt files. For example, if we look at the first file in fullpath1 named tree1 then I want to go in to that file and extract some of the columns and write it to a newfile also called tree1. The same for tree2, tree3... and so on... So that I in the end have the same file names (tree1, tree2, tree3...) only containing the extracted columns from fullpath1.

Recommended Answers

All 5 Replies

For almost all my path manipulations, I use a module with a path class. This path class was originaly written by developpers of the python language, I added a few useful features. Here is the attached module. Save it as whatever.py and use it. Here is an example:

from whatever import path
pathA = path("~").hard/"foo"/"bar"
pathB = path("~").hard/"hello"/"world"
for src in pathA.listdir():
    if src.ext != ".txt":
        continue
    dst = pathB/src.name
    with open(dst) as ofh:
        with open(src) as lines:
            for line in lines:
                # get some info
                ofh.write("something")

The module contains many many features plus a few unix things and a few personal things, but it should run very well for most things and save you a lot of time with file system paths.

Im pretty new to programming and Python. So could you perhaps explain some what the code exactly does?

Thanks.

Im pretty new to programming and Python. So could you perhaps explain some what the code exactly does?

Thanks.

Ok, here is the same code with some comments

# import the 'path' class from the module I posted, which you saved as
# whatever.py (but you can choose another name)
from whatever import path
# path("~").hard creates a path to your home folder. Print it to see what it does
# for me it's: path('/home/eric')
# then dividing by "foo" gives me path('/home/eric/foo')
# dividing again by "bar" gives me path('/home/eric/foo/bar')
# the path will be ok independently of your os
pathA = path("~").hard/"foo"/"bar"
pathB = path("~").hard/"hello"/"world"
# this for loop iterates over the files in the folder pathA
# for me, src takes the values
# /home/eric/foo/bar/world.png, /home/eric/foo/bar/hello.txt, etc
for src in pathA.listdir():
    if src.ext != ".txt": # skip files without extension .txt
        continue
    dst = pathB/src.name # this is /home/eric/hello/world/world.png for example
    with open(dst, "w") as ofh: # open file dst for writing
        with open(src) as lines: # open file src for reading
            for line in lines:
                # get some info
                ofh.write("something") # write to dst file

It's working! Thanks!

It's working! Thanks!

Great !

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.