Pass a file with filepaths to Python in Ubuntu terminal to analyze each file? - python-3.x

I have a text file with file paths:
path1
path2
path3
...
path100000000
I have my python script app.py that should run on each file (path1, path2 ...)
Please advise what is the best way to do it?
Should I just get it as argument, and then:
with open(input_file, "r") as f:
lines = f.readlines()
for line in lines:
main_function(line)

Yes that should work, except readlines() doesn't remove newline characters.
with open(input_file, "r") as f:
lines = f.readlines()
for line in lines:
main_function(line.strip())
**Note: The above code assumes the file is in the same directory as the python script file.
You are using context managers. Hence, place the code inside the context.
So according to your comment,
If you want to pass filename where you will read the file contents in the main_function, then the above code will work.
If you want to read the file and then pass the file contents, then you will have to modify the above code to first read the content and then pass it to the function
with open(input_file, "r") as f:
lines = f.readlines()
for line in lines:
main_function(open(line.strip(), "r").read())
**Note: the above function will read the whole file as a single string (text)

Related

Deleting from files

(I have only been learning Python3 for about 2 weeks now. So if you could keep the answer as ELI5 as possible that would be great)
In this image, I have first read the file and then second zeroed the file and recreated it while removing all the lines containing 'Ford'
This image shows the contents of the file that I wish to be printed out
The problem I have is that the result is being printed as 'none'.
How would I make it so this program prints out what is inside the file?
Thanks!
code below
def delete_ford(path, term):
buffer = []
with open(path, "r") as file:
for line in file:
buffer.append(line.strip())
with open(path, "w") as file:
for line in buffer:
if line != term:
file.write(line + "\n")
with open(path, "r") as file:
for line in file:
buffer.append(line.strip())
print(buffer)
print(delete_ford("cars.txt", "Ford"))
As #John Zwinck and #Albert Alberto pointed out, your function has nothing to return as the output is written to the file.
If you want it simply to print out the contents of the file, you can do this when you're writing to it like this:
with open(path, "w") as file:
for line in buffer:
if line != term:
file.write(line + "\n") # This is what is being written to the file
print(line) # So this will effectively output the contents of the file
Hope this does what you want it to

How do I copy part of a file to a new file?

In Python I'd like to open a text file as file, and copy only part of the file to a new file. For example, I want to copy only part of the file, say between the line EXAMPLE\n and line END\n. So I want to delete everything before line EXAMPLE\n and everything after line END\n. How can I do that?
I can read the file using the following code, but how do I delete the
with open(r'filepath\myfile.txt', 'r') as f:
file = f.readlines()
<delete unwanted lines in file>
with open(r'filepath\newfile.txt', 'r') as f:
f.writelines(file)
Create a new array and only add the lines you want to that array:
new_lines = []
found_example=False
found_end=False
for line in file:
if line == "EXAMPLE\n": found_example=True
if line == "END\n": found_end=True
if found_example != found_end: new_lines.append(line)
file = new_lines
Now just write file to your file and you are done. Note that in your example you didn't open the file in write mode, so it would look more like this:
with open(r'filepath\newfile.txt', 'w+') as f:
f.writelines(file)
Read each line and notice whether it contains EXAMPLE or END. In the former case, set a flag to start outputting lines; in the latter, set the same flag to stop.
process = False
with open('myfile.txt') as f, open('newfile.txt', 'w') as g:
for line in f:
if line == 'EXAMPLE\n':
process = True
elif line == 'END\n':
process = False
else:
pass
if process:
line = line.strip()
print (line, file=g)

Python3, read specific word from .txt file

I want to know how I can search for a specific word in a .txt file using Python3.
If the word 'pizza' is included in the .txt file, then it should (e.g.) import another python program.
You can iterate through each line in the file with a 'for' loop and check to see if the string is in the line:
f = open('file.txt', 'r')
for line in f:
if 'pizza' in line:
# do whatever you want here
print("Found it!")
else:
pass

Python - Spyder 3 - Open a list of .csv files and remove all double quotes in every file

I've read every thing I can find and tried about 20 examples from SO and google, and nothing seems to work.
This should be very simple, but I cannot get it to work. I just want to point to a folder, and replace every double quote in every file in the folder. That is it. (And I don't know Python well at all, hence my issues.) I have no doubt that some of the scripts I've tried to retask must work, but my lack of Python skill is getting in the way. This is as close as I've gotten, and I get errors. If I don't get errors it seems to do nothing. Thanks.
import glob
import csv
mypath = glob.glob('\\C:\\csv\\*.csv')
for fname in mypath:
with open(mypath, "r") as infile, open("output.csv", "w") as outfile:
reader = csv.reader(infile)
writer = csv.writer(outfile)
for row in reader:
writer.writerow(item.replace("""", "") for item in row)
You don't need to use csv-specific file opening and writing, I think that makes it more complex. How about this instead:
import os
mypath = r'\path\to\folder'
for file in os.listdir(mypath): # This will loop through every file in the folder
if '.csv' in file: # Check if it's a csv file
fpath = os.path.join(mypath, file)
fpath_out = fpath + '_output' # Create an output file with a similar name to the input file
with open(fpath) as infile
lines = infile.readlines() # Read all lines
with open(fpath_out, 'w') as outfile:
for line in lines: # One line at a time
outfile.write(line.replace('"', '')) # Remove each " and write the line
Let me know if this works, and respond with any error messages you may have.
I found the solution to this based on the original answer provided by u/Jeff. It was actually smart quotes (u'\u201d') to be exact, not straight quotes. That is why I could get nothing to work. That is a great way to spend like two days, now if you'll excuse me I have to go jump off the roof. But for posterity, here is what I used that worked. (And note - there is the left curving smart quote as well - that is u'\u201c'.
mypath = 'C:\\csv\\'
myoutputpath = 'C:\\csv\\output\\'
for file in os.listdir(mypath): # This will loop through every file in the folder
if '.csv' in file: # Check if it's a csv file
fpath = os.path.join(mypath, file)
fpath_out = os.path.join(myoutputpath, file) #+ '_output' # Create an output file with a similar name to the input file
with open(fpath) as infile:
lines = infile.readlines() # Read all lines
with open(fpath_out, 'w') as outfile:
for line in lines: # One line at a time
outfile.write(line.replace(u'\u201d', ''))# Remove each " and write the line
infile.close()
outfile.close()

How do I delete line starting with ">" while reading a file to string in Python

I want to read all files ending with ".fasta" in mydir directory one by one and save content except lines starting with ">" to a string called "data" for further analysis while also ignoring newline characters. So far I have this:
for file in os.listdir(mydir):
if file.endswith(".fasta"):
with open(file, 'r') as myfile:
data = myfile.read().replace('\n', '')
How do I read file into a string AND in the same command skip all lines starting with ">"?
Here you go
for file in os.listdir(mydir):
if file.endswith(".fasta"):
with open(file, 'r') as myfile:
data = "".join(line for line in myfile if line[:1]!='>')

Resources