Hi ALL,

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

Recommended Answers

All 5 Replies

I am lost with your explanations.

suppose i have two files say x.data1 x.data2. notice that the file names are described by a varible name 'x' with two extension. We usually handle such kind of files in shell script as $x.data1 and $x.data2. Therefore, if I know the type of extensions of my file name and if I define those in my program then just giving one input 'x', the two files are accessable. How to do that?

I have no idea about it.

commented: This comment does not contribute to discussion -3
commented: no one is forcing you to answer +0

You are not using the imports from the first 4 lines so those do not count as effort. To generate strings with incrementing number you can use:

names = ['test%i.data%i' % (m,n) for m in range(1, 5) for n in range(1,3)]
print names
"""Output:
['test1.data1', 'test1.data2', 'test2.data1', 'test2.data2', 'test3.data1', 'test3.data2', 'test4.data1', 'test4.data2']
"""

if i understand you correctly, you have files, test1.data1 in directory test1 and test2.data2 in directory test2, you want to copy the items in the files to test3.data3 in directory test3 and test4.data4 to directory test4 if so then the following code should help

filename = raw_input('enter the input file name: ') # get the name of the file
filename = str(filename) # save the name of the file at a string 
_dir = 'test' # the basic directory name 

count = 1 # a variable to help you count 

while count < 3: # to have sure that the whole process goes 2 rounds 

# get the directory and copy the file into memory 
    f = file(_dir + str(count) + '/'+ filename + str(count) +'.data' + str(count), 'r')

# read the content of the file into x
    x = f.read()
    f.close() # close the file

    f = file(_dir + str((count + 2)) + '/'+ filename + str((count + 2)) +'.data' + str((count + 2)), 'w')
    f.write(x)
    f.close()

    this code was writen with python 2.7
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.