Looping over lines in a file and creating multiple directory - python

I am trying to loop over the lines in a file and create multiple directories. My script is working only for the first line of list in a file. Here is my script. I have attached image of list as well. That is for both list_bottom.dat and list_top.dat.
import os
f = open("list_top.dat", "r")
g = open("list_bottom.dat", "r")
for lines in f:
m_top = lines.split()[0]
m_bot = lines.split()[0]
os.mkdir(m_top)
os.chdir(m_top)
for lines in g:
print(lines)
m_bot = lines.split()[0]
print(m_bot)
os.mkdir(m_top + "_" + m_bot)
os.chdir(m_top + "_" + m_bot)
for angle in range(5):
os.mkdir(m_top + "_" + "m_bot" + "_angle_" + str(angle))
os.chdir(m_top + "_" + "m_bot" + "_angle_" + str(angle))
os.chdir("../")
os.chdir("../")
os.chdir("../")
os.chdir("../")

you are trying to read from a file pointer, not from its content. you should do this instead
with open("file.txt") as f:
lines = f.readlines()
for line in lines:
do_stuff()
(for readability i don't post this as a comment, but that's a comment)

Related

How to space items?

import os
s = os.listdir("qwe")
f = open("asd.txt", "w")
for i in range(0, 100):
try:
f.writelines(s[i] + ":" + "\n")
f.writelines(os.listdir("qwe\ ".strip() + s[i] + "\Wallets"))
f.writelines("\n" + "\n")
except:
continue
It prints data like this:
dsadasda:
ada.txtli.pysda.txt
elele:
erti:
file.txt
jhgjghjgh:
new.txtpy.py
lolo:
sdada:
If there are lots of things in wallet it prints them together, how can i space between them?
you may write your code as this way
import os
s = os.listdir("qwe")
try:
with open("asd.txt", "a") as f: # opening file once
for i in range(0, 100):
f.writelines(s[i] + ":" + "\n")
print(s[i] + ":" + "\n")
f.writelines(os.listdir("qwe\ ".strip() + s[i] + "\Wallets"))
print(os.listdir("qwe\ ".strip() + s[i] + "\Wallets"))
f.writelines("\n" + "\n")
except:
pass
this is happening because you open the file for each loop so it doesn't write anything in your file
,
another thing that you are opening file in writing mode which means that it will erase the content of the file and replace it with the new one

not writing all lines of output in a new file in python

I am creating a file and I want to write all lines of write_line to my output.
With this could I have a new file but only with the last line of write_log not all the lines. I think I should have a for before written log and tell to write all, but i am so new with python and need help.
I am getting name / familtname / id by SOAP response. I want to print responses which are in lines, now i just see the last line not all the lines.
timestamp = str(datetime.datetime.now())[:19]
file = open(CreateFile, 'w')
write_line = str(name).strip() + ';' + familyname.strip() + ';' + str(id).strip() + ';' + timestamp
file.writelines(write_line + '\n')
def CreateFile():#******************creating output log file*****
today = str(datetime.date.today()).split('-')
NowTime = str(datetime.datetime.now())[11:19:]
Nowtime_split = NowTime.split(':')
timestamp=Nowtime_split[0]+Nowtime_split[1]+Nowtime_split[2]
daystamp=today[0]+today[1]+today[2]
filename = 'log' + '_' + daystamp + '_' + timestamp + '.csv'
destfile = r'C:\Desktop' + str(filename)
file = open(destfile, 'w')
file.close()
return(destfile)
CreateFile=CreateFile()
this is a small case:
import datetime
timestamp = str(datetime.datetime.now())[:19]
file = open('1.txt', 'w')
for i in range(10):
write_line ='try'+str(i)
file.writelines(write_line + '\n')
file.close()
`
I'm not really sure what you want but I think the problem is because you're using write parameter to open the file and it's always replacing the previous text, so what you can do is replacing write with append(a):
timestamp = str(datetime.datetime.now())[:19]
with open(CreateFile, 'a') as file:
write_line = str(name).strip() + ';' + familyname.strip() + ';' +str(id).strip() + ';' + timestamp
file.write(write_line + '\n')
I suggest you to use with open... in order to avoid closing the file opened and other futures errors
lines = ['line1', 'line2', ...] # set of lines (list) you want to add in the current timestamp file
with open('current_timestampfile.txt', 'w') as f:
f.writelines("%s\n" % l for l in lines)

Loop prints multiple lines (as intended) but only writes one line to the file

The loop reads all text files in the directory and prints the value from the first line of each text file (as expected) but only writes one line to the file.
I have tried several variants of the loop using for and while loops but I think I may be doing them wrong. I am thrown off because it prints the correct output (several lines) but only writes one line.
# this program reads the files in this directory and gets the value in the first line of each text file
# it groups them by the first two numbers of their filename
import glob
# get list of all text files in directory
path = "./*.txt"
txt_files = glob.glob(path)
# these are the groups I need to sort the results by
a1 = "10"
a2 = "20"
b1 = "30"
c1 = "40"
# list of files is in txt_files
for fileName in txt_files:
# get the two digits from the filename to group the files
device = fileName[2:4]
# if the file name's first two digits (device) match the variable, open the file and get the value in the first line
if device == a1:
file = open(fileName)
line = file.readline()
# then, write that first line's value to the usage.txt file
print(device + "_" + line)
fileU = open("usage.txt", 'w')
fileU.write(device + "_" + line + "\n")
file.close()
# if the file name's first two digits = 20, proceed
elif device == a2:
# open the text file and get the value of the first line
file = open(fileName)
line = file.readline()
print(device + "_" + line)
fileU = open("usage.txt", 'w')
fileU.write(device + "_" + line + "\n")
file.close()
# if the file name's first two digits = 30, proceed
elif device == b1:
file = open(fileName)
line = file.readline()
print(device + "_" + line)
fileU = open("usage.txt", 'w')
fileU.write(device + "_" + line + "\n")
file.close()
The expected results would be usage.txt showing the same output as what is printed in the console.
usage.txt will only have one line: 30_33
the console will print all of the lines:
10_36
10_36
20_58
20_0
20_58
30_33
30_33
Process finished with exit code 0
You are opening and truncating the file, open with append:
Since you open the file each time you loop, and you are not using a, you are truncating the file each loop so each loop you write a new 1 line file.
fileU = open("usage.txt", 'a')
You're recreating the file each time you open it in the loop. You should open it once before the loop.
with open("usage.txt", "w") as fileU:
for fileName in txt_files:
# get the two digits from the filename to group the files
device = fileName[2:4]
# if the file name's first two digits (device) match the variable, open the file and get the value in the first line
if device == a1:
file = open(fileName)
line = file.readline()
# then, write that first line's value to the usage.txt file
print(device + "_" + line)
fileU.write(device + "_" + line + "\n")
file.close()
# if the file name's first two digits = 20, proceed
elif device == a2:
# open the text file and get the value of the first line
file = open(fileName)
line = file.readline()
print(device + "_" + line)
fileU.write(device + "_" + line + "\n")
file.close()
# if the file name's first two digits = 30, proceed
elif device == b1:
file = open(fileName)
line = file.readline()
print(device + "_" + line)
fileU.write(device + "_" + line + "\n")
file.close()

Opening and editing multiple files in a folder with python

I am trying to modify my .fasta files from this:
>YP_009208724.1 hypothetical protein ADP65_00072 [Achromobacter phage phiAxp-3]
MSNVLLKQ...
>YP_009220341.1 terminase large subunit [Achromobacter phage phiAxp-1]
MRTPSKSE...
>YP_009226430.1 DNA packaging protein [Achromobacter phage phiAxp-2]
MMNSDAVI...
to this:
>Achromobacter phage phiAxp-3
MSNVLLKQ...
>Achromobacter phage phiAxp-1
MRTPSKSE...
>Achromobacter phage phiAxp-2
MMNSDAVI...
Now, I've already have a script that can do it to a single file:
with open('Achromobacter.fasta', 'r') as fasta_file:
out_file = open('./fastas3/Achromobacter.fasta', 'w')
for line in fasta_file:
line = line.rstrip()
if '[' in line:
line = line.split('[')[-1]
out_file.write('>' + line[:-1] + "\n")
else:
out_file.write(str(line) + "\n")
but I can't get to automate the process for all 120 files in my folder.
I tried using glob.glob, but I can't seem to make it work:
import glob
for fasta_file in glob.glob('*.fasta'):
outfile = open('./fastas3/'+fasta_file, 'w')
with open(fasta_file, 'r'):
for line in fasta_file:
line = line.rstrip()
if '[' in line:
line2 = line.split('[')[-1]
outfile.write('>' + line2[:-1] + "\n")
else:
outfile.write(str(line) + "\n")
it gives me this output:
A
c
i
n
e
t
o
b
a
c
t
e
r
.
f
a
s
t
a
I managed to get a list of all files in the folder, but can't open certain files using the object on the list.
import os
file_list = []
for file in os.listdir("./fastas2/"):
if file.endswith(".fasta"):
file_list.append(file)
Considering you are able to change the contents of file name now you need to automate the process. We changed the function for one file by removing file handler which was used twice for the opening of file.
def file_changer(filename):
data_to_put = ''
with open(filename, 'r+') as fasta_file:
for line in fasta_file.readlines():
line = line.rstrip()
if '[' in line:
line = line.split('[')[-1]
data_to_put += '>' + str(line[:-1]) + "\n"
else:
data_to_put += str(line) + "\n"
fasta_file.write(data_to_put)
fasta_file.close()
Now we need to iterate over all your files. So lets use glob module for it
import glob
for file in glob.glob('*.fasta'):
file_changer(file)
You are iterating the file name, which gives you all the characters in the name instead of the lines of the file. Here is a corrected version of the code:
import glob
for fasta_file_name in glob.glob('*.fasta'):
with open(fasta_file_name, 'r') as fasta_file, \
open('./fastas3/' + fasta_file_name, 'w') as outfile:
for line in fasta_file:
line = line.rstrip()
if '[' in line:
line2 = line.split('[')[-1]
outfile.write('>' + line2[:-1] + "\n")
else:
outfile.write(str(line) + "\n")
As an alternative to the Python script, you can simply use sed from the command line:
sed -i 's/^>.*\[\(.*\)\].*$/>\1/' *.fasta
This will modify all files in place, so consider copying them first.

Adding filename to last column in csv using python

I have a folder full of .mpt files, each of them having the same data format.
I need to delete the first 57 lines from all files and append these files into one csv - output.csv.
I have that section already:
import glob
import os
dir_name = 'path name'
lines_to_ignore = 57
input_file_format = '*.mpt'
output_file_name = "output.csv"
def convert():
files = glob.glob(os.path.join(dir_name, input_file_format))
with open(os.path.join(dir_name, output_file_name), 'w') as out_file:
for f in files:
with open(f, 'r') as in_file:
content = in_file.readlines()
content = content[lines_to_ignore:]
for i in content:
out_file.write(i)
print("working")
convert()
print("done")
This part works ok.
how do i add the filename of each .mpt file as the last column of the output.csv
Thank you!
This is a quick 'n dirty solution.
In this loop the variable i is just a string (a line from a CSV file):
for i in content:
out_file.write(i)
So you just need to 1) strip off the end of line character(s) (either "\n" or "\r\n") and append ",".
If you're using Unix, try:
for i in content:
i = i.rstrip("\n") + "," + output_file_name + "\n"
out_file.write(i)
This assumes that the field separator is a comma. Another option is:
for i in content:
i = i.rstrip() + "," + output_file_name
print >>out_file, i
This will strip all white space from the end of i.
Add quotes if you need to quote the output file name:
i = i.rstrip(...) + ',"' + output_file_name '"'
The relevant part:
with open(f, 'r') as in_file:
content = in_file.readlines()
content = content[lines_to_ignore:]
for i in content:
new_line = ",".join([i.rstrip(), f]) + "\n" #<-- this is new
out_file.write(new_line) #<-- this is new

Categories

Resources