1

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

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

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

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

2
  • 2
    but I can't complete it - Why not? What are you stuck on? Commented Jan 29, 2013 at 2:32
  • I am new in python. I am stucked at accessing directories and then accessing files. Commented Jan 29, 2013 at 2:42

1 Answer 1

4

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.data1 which 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'

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Two input suppose input.data1 and input.data2. How do I access these two data files only by passing 'input' string. In shell script if I define i=input then the two files are $i.data1 and $i.data2. What will be the syntax in python
you're saying if you have i = raw_input() where the input is the string "input"? In that case you concatinate i+".data1" to get the string "input.data1" or were you trying to not hardcode it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.