使用python访问目录和文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14574705/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 11:52:14  来源:igfitidea点击:

Accessing directories and files using python

python

提问by user1964587

I want to analyze my data by a python script. The data are in multiple file and they are kept in different directories. Moreover, the name of the files in those directories are same but have different extension. I want to get my output just by providing a single input filename. for example

我想通过 python 脚本分析我的数据。数据在多个文件中,它们保存在不同的目录中。此外,这些目录中的文件名称相同但扩展名不同。我只想通过提供一个输入文件名来获得我的输出。例如

my filename is 'test' and this will be my input

我的文件名是“test”,这将是我的输入

the actual filenames are suppose test.data1, test.data2, test.data3, test.data4 and among them two input files such as test1.data1 and test2.data2 are kept in test1, test2 directories. The test3 and test4 are output directories.My aim is access those directories through the python script and then access those datafile. All the four directories are present in my machine but the two output files will be generated with extension .data3 and .data4 through the script. I have started with this following script but I can't complete it. Any help will be appreciating

实际的文件名假设为 test.data1、test.data2、test.data3、test.data4,其中 test1.data1 和 test2.data2 两个输入文件保存在 test1、test2 目录中。test3 和 test4 是输出目录。我的目标是通过 python 脚本访问这些目录,然后访问这些数据文件。所有四个目录都存在于我的机器中,但将通过脚本生成两个输出文件,扩展名为 .data3 和 .data4。我已开始使用以下脚本,但无法完成。任何帮助将不胜感激

import re
import numpy
import os
import glob
filename =raw_input('enter the input file name: ')
lines = open(input.data1, 'r').readlines()
lines1 = open(input.data2, 'r').readlines()
outfile1=open(input.data3, 'w')
outfile2=open(input.data4, 'w')

Best Sudipta

最好的苏迪普塔

回答by Mike

OK, so you're having problems accessing files and directories... if you want to see what files are in a directory you can do something like this:

好的,所以您在访问文件和目录时遇到了问题……如果您想查看目录中的文件,您可以执行以下操作:

for files in os.listdir("./Downloads"):
  print files

If you want to get a file name from the user and open that file for reading:

如果您想从用户那里获取文件名并打开该文件进行阅读:

filename = raw_input("what is the file name? ")
try:
    fp = open(filename, "r")
catch:
    print "couldn't open file"

One of the main problems in your code right now is you ask for a file name:

现在您代码中的主要问题之一是您要求输入文件名:

filename =raw_input('enter the input file name: ')

Then you never use it. You instead try to open input.data1which is not even defined in your program. If you want to open a file with a hardcoded path you can do: 'input.data1'assuming your file is called 'input.data1'

然后你永远不会使用它。相反,您尝试打开input.data1甚至未在您的程序中定义的内容。如果你想用硬编码路径打开一个文件,你可以这样做:'input.data1'假设你的文件被称为“input.data1”

You can also open a file at a specific path hardcoded such as:

您还可以在硬编码的特定路径上打开文件,例如:

lines = open("./input/data1", "r").readlines()

Hope that helps, your question wasn't very specific so it's hard to give a better answer.

希望有所帮助,您的问题不是很具体,因此很难给出更好的答案。