Python文件命名问题

发布于 2024-12-01 08:22:07 字数 1072 浏览 0 评论 0 原文

我正在尝试迭代多个 .rtf 文件,并针对每个文件:读取文件,执行一些操作,然后将新文件作为纯文本文件写入子目录,其名称与原始文件相同,但扩展名为 .rtf。 txt 扩展名。我遇到的问题是文件命名。

如果文件名为 foo.rtf,我希望子目录中的新文件为 foo.txt。这是我的代码:

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)

当我运行此代码时,出现类型错误:

类型错误:强制转换为 Unicode:需要字符串或缓冲区,未找到任何类型

如何修复代码以将新文件写入子目录并将文件扩展名从 .rtf 更改为 .txt?感谢您的帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

茶底世界 2024-12-08 08:22:07

代替 item.replace,查看 os.path 模块中的一些函数 (http://docs.python.org/library/os.path.html)。它们是为分割和重新组合文件名的各个部分而设计的。例如,os.path.splitext 会将文件名拆分为文件路径和文件扩展名。

假设您有一个文件 /tmp/foo.rtf 并且您想将其移动到 /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)

或者如果您想要单行版本:

old_file = '/tmp/foo.rtf'
new_file = '%s%s' % (os.path.splitext(old_file)[0],'.txt')

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')
坐在坟头思考人生 2024-12-08 08:22:07

我从未使用过 glob,但这里有一种不使用模块的替代方法:
您可以轻松地使用删除后缀

name = name[:name.rfind('.')]

,然后添加新后缀:

name = name + '.txt'

为什么不使用函数?

def change_suffix(string, new_suffix):
    i = string.rfind('.')
    if i < 0:
        raise ValueError, 'string does not have a suffix'
    if not new_suffix[0] == '.':
        new_suffix += '.'
    return string[:i] + new_suffix

I've never used glob, but here's an alternative way without using a module:
You can easily strip the suffix using

name = name[:name.rfind('.')]

and then add the new suffix:

name = name + '.txt'

Why not using a function ?

def change_suffix(string, new_suffix):
    i = string.rfind('.')
    if i < 0:
        raise ValueError, 'string does not have a suffix'
    if not new_suffix[0] == '.':
        new_suffix += '.'
    return string[:i] + new_suffix
撩心不撩汉 2024-12-08 08:22:07

glob.iglob() 生成不带字符“*”的路径名。
因此你的行应该是:

new_file_name = item.replace('.rtf', '.txt') 

考虑使用更清晰的名称(保留“文件名”作为文件名,并使用“路径”作为文件的完整路径;使用“path_original”而不是“item”),os.extsep(“. ' 在 Windows 中)和 os.path.splitext():

path_txt = os.extsep.join([os.path.splitext(path_original)[0], 'txt'])

现在是最好的提示:
numpy 可能可以直接读取你的文件:(

data = np.genfromtxt(filename, unpack=True)

另请参阅此处

为了更好地了解 TypeError 的来源,请将代码包装在以下 try/ except 块中:

try:
    (your code)
except:
    import traceback
    traceback.print_exc()

glob.iglob() yields pathnames, without the character '*'.
therefore your line should be:

new_file_name = item.replace('.rtf', '.txt') 

consider working with clearer names (reserve 'filename' for a file name and use 'path' for a complete path to a file; use 'path_original' instead of 'item'), os.extsep ('.' in Windows) and os.path.splitext():

path_txt = os.extsep.join([os.path.splitext(path_original)[0], 'txt'])

now the best hint of all:
numpy can probably read your file directly:

data = np.genfromtxt(filename, unpack=True)

(see also here)

To better understand where your TypeError comes from, wrap your code in the following try/except block:

try:
    (your code)
except:
    import traceback
    traceback.print_exc()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文