Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6369855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:51:25+00:00 2026-05-25T00:51:25+00:00

I am trying to iterate through a number .rtf files and for each file:

  • 0

I am trying to iterate through a number .rtf files and for each file: read the file, perform some operations, and then write new files into a sub-directory as plain text files with the same name as the original file, but with .txt extensions. The problem I am having is with the file naming.

If a file is named foo.rtf, I want the new file in the subdirectory to be foo.txt. here is my code:

import glob
import os
import numpy as np


dir_path = '/Users/me/Desktop/test/'
file_suffix = '*.rtf'
output_dir = os.mkdir('sub_dir')
for item in glob.iglob(dir_path + file_suffix):
    with open(item, "r") as infile:
        reader = infile.readlines()
        matrix = []
        for row in reader:
            row = str(row)
            row = row.split()
            row = [int(value) for value in row]
            matrix.append(row)
        np_matrix = np.array(matrix)
        inv_matrix = np.transpose(np_matrix)
        new_file_name = item.replace('*.rtf', '*.txt') # i think this line is the problem?
        os.chdir(output_dir)
        with open(new_file_name, mode="w") as outfile:
            outfile.write(inv_matrix)

When I run this code, I get a Type Error:

TypeError: coercing to Unicode: need string or buffer, NoneType found

How can I fix my code to write new files into a subdirectory and change the file extensions from .rtf to .txt? Thanks for the help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T00:51:26+00:00Added an answer on May 25, 2026 at 12:51 am

    Instead of item.replace, check out some of the functions in the os.path module (http://docs.python.org/library/os.path.html). They’re made for splitting up and recombining parts of filenames. For instance, os.path.splitext will split a filename into a file path and a file extension.

    Let’s say you have a file /tmp/foo.rtf and you want to move it to /tmp/foo.txt:

    old_file = '/tmp/foo.rtf'
    (file,ext) = os.path.splitext(old_file)
    print 'File=%s Extension=%s' % (file,ext)
    new_file = '%s%s' % (file,'.txt')
    print 'New file = %s' % (new_file)
    

    Or if you want the one line version:

    old_file = '/tmp/foo.rtf'
    new_file = '%s%s' % (os.path.splitext(old_file)[0],'.txt')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What I'm trying to do is iterate through a repeater and read some controls
I'm trying to write some code to look through an image file for groups
I am trying to iterate through some number as 02100021, this is a routing
I am trying to use JQuery to iterate through a number of <li> objects
I'm trying to iterate through an array of elements. jQuery's documentation says: jquery.Each() documentation
I'm trying to iterate through a set of keys in a properties file, so
I'm trying to read in a text file line by line and process each
I am trying to iterate through the SPListItem.Versions collection to find the latest approved
I'm trying to iterate through all nodes, so I can print them out for
I'm trying to iterate through all the empty textboxes in a table and change

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.