Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 创建文件目录的.txt文件_Python_Python 2.7 - Fatal编程技术网

Python 创建文件目录的.txt文件

Python 创建文件目录的.txt文件,python,python-2.7,Python,Python 2.7,我正在尝试在某个位置创建文件目录的.txt文件,删除前缀并保存文本文件 我使用os.walk模块将一个位置的目录列表构建到一个.txt文件中。我总是得到目录的文本文件 在下一段代码中删除这些目录行前缀的部分不起作用。它创建了自己的.txt文件(正如它应该创建的那样),但它总是空的 如果有一个解决方案可以在一个.txt文件和一段代码中完成所有这些,那就更好了 这是我到目前为止所拥有的,出于隐私考虑,我正在使用虚拟目录 import os from datetime import date

我正在尝试在某个位置创建文件目录的.txt文件,删除前缀并保存文本文件

我使用
os.walk
模块将一个位置的目录列表构建到一个.txt文件中。我总是得到目录的文本文件

在下一段代码中删除这些目录行前缀的部分不起作用。它创建了自己的.txt文件(正如它应该创建的那样),但它总是空的

如果有一个解决方案可以在一个.txt文件和一段代码中完成所有这些,那就更好了

这是我到目前为止所拥有的,出于隐私考虑,我正在使用虚拟目录

 import os
    from datetime import datetime

    # this is to create a filename with the timestamp_directory_list for a .txt file
    now = datetime.now()
    filename = datetime.now().strftime("%Y_%m_%d_%H_%M_%S_directory_list.txt")


    # uses os module to walk the directories and files 
# within a given location, then writes it line by line to a .txt file
    with open(filename, "w") as directory_list:
        for path, subdirs, files in os.walk(r"C:/Users"):
            for filenameX in files:
                f = os.path.join(path)
                directory_list.write(str(f) + os.linesep)


    # Open up .txt file, read a line, trim the prefix, then save it
    # this is to create a filename with the timestamp_directory_list for a .txt file
    trim = datetime.now().strftime("%Y_%m_%d_%H_%M_%S_trimmed_directories.txt")

    def remove_prefix(text, prefix):
        # Remove prefix from supplied text string
        if prefix in text:
            return text[len(prefix):]
        return text

    with open(filename, "r") as trim_input, \
        open(trim, "a") as trim_output:

        for line in trim_input:
            print line
            if "C" in line:
                print line
                trim_output = remove_prefix(trim_input, 'C')
                trim_output.write(line+ os.linesep) 

你把变量名弄混了,实际上我希望如果运行它会引发一些异常

  • 输出文件和修剪的行都有
    trim\u输出
  • 您正在调用“输入文件对象”上的
    remove\u prefix
    ,而不是
    行上的
  • 您得到了修剪过的行(我认为是覆盖了输出文件refrefreference),但是您将(未修剪过的)行写入了输出文件
您的代码应该类似于

with open(filename, "r") as trim_input, \
        open(trim, "a") as trim_output:
    for line in trim_input:
        print line
        if "C" in line: 
            # this if is a bit useless you have an another if inside the remove_prefix, 
            # also you are skyping all the lines without prefix
            print line
            trimmed_line = remove_prefix(line, 'C')
            trim_output.write(trimmed_line+ os.linesep) 
以后编辑: 为了使代码的行为符合初始描述中的规定,“如果”不应出现,并且代码不包含一个级别

此外,
remove_前缀
也有缺陷

def remove_prefix(text, prefix):
    # Remove prefix from supplied text string
    if prefix in text: 
        # if prefix is "C", this is true for "Ctest" and also for "testC"
        return text[len(prefix):] # but it removes the first chars
    return text
应该是

def remove_prefix(text, prefix):
    # Remove prefix from supplied text string
    if text and text.startswith(prefix): 
        return text[len(prefix):]
    return text

这里有一个与你类似的问题:谢谢!那么这是进行这一部分的最佳方式吗?或者将实际前缀移动到def remove_prefix块是一种更干净的方法,这样做时不会跳过没有前缀的行吗?在我的回答中,我刚刚“更正”了您的代码,保留了当前的“逻辑”。看看它应该做什么的描述。。。是的,if不应出现在本节中,因为如果存在,则在输出文件中只会得到带有前缀的行