I want to format an existing text file, the contents of text file are:
Aurangabad
Adilabad
Beed
I want to format it like:
Aurangabad|Aurangabad,
Adilabad|Adilabad,
Beed|Beed,
I am not so good in Python file handling.
the code to do so:
with open('file_name.txt','r') as file:
list_of_lines = file.readlines()
new_lines_list = []
for line in list_of_lines:
line = line.replace('\n','') #because each line end with this and we don't need it now (\n is the newline chr)
new_lines_list.append('{0}|{0}\n'.format(line)) #the same as - new_lines_list.append(line+'|'+line+'\n')
with open('file_name.txt','w') as file:
string_to_write = ''.join(new_lines_list)
file.write(string_to_write)
if you don't understand the with statement: it is basically to open the file and at the end it will close itself (and even if some exception occur it will still close (I explain bad if you don't understand go here)
Related
I am reading a file which contains json data and in between it contains other text.So for that i want to check that condition on reading the file if line starts with condition how can i achieve this?
with open ("inputfile.txt") as f:
content = f.read().replace('}U','},')[::-1].replace(',', '', 1)].replace(":[",":").replace("]","")
content = '[{}]'.format(content)
data=json.loads(content)
I want to check the file if the line starts with condition like this
startswith("{"+"\"M\""+":")
I Have tried reading line by line and checking if the line startswith condition but for large files it is tak
inputfile.txt
sometext
{"M":{"1":"data","2":"data2"}}U
asdklaasd
{"M":{"3":"555","5":"3333"}}U
I want to read the lines only that start with {"M":
Output I need is like this
[{"M":{"1":"data","2":"data2"}},{"M":{"3":"555","5":"3333"}}]
I want to download multiple specific links(images´ urls) into a txt file(or any file where all links can be listed underneath each others).
I get them but the code wrtite each link on the top of the other one and at the end it stays only a link :(. Also I want not repeated urls
def dlink(self, image_url):
r = self.session.get(image_url, stream=True)
with open('Output.txt','w') as f:
f.write(image_url + '\n')
The issue is most simply that opening a file with mode 'w' truncates any existing file. You should change 'w' to 'a' instead. This will open an existing file for writing, but append instead of truncating.
More fundamentally, the problem may be that you are opening the file over and over in a loop. This is very inefficient. The only time the approach you use could be really useful is if your program is approaching the OS-imposed limit on number of open files. If this is not the case, I would recommended putting the loop inside the with block, keeping the mode as 'w' since you open the file just once now, and passing the open file to your dlink function.
Edit
Huge mistake of my part, as it is a method, and you will call it several times, if you open it in write mode ('w') or similar, it will Overwrites the existing file if the file exists.
So, if you use the 'a' way, you can see that:
Opens a file for appending. The file pointer is at the end of the file
if the file exists. That is, the file is in the append mode. If the
file does not exist, it creates a new file for writing.
The other problem radics in image_url is a list, so you need to write it line by line:
def dlink(self, image_url):
r = self.session.get(image_url, stream=True)
with open('Output.txt','a') as f:
for url in list(set(image_url)):
f.write(image_url + '\n')
another way to do it:
your_file = open('Output.txt', 'a')
r = self.session.get(image_url, stream=True)
for url in list(set(image_url)):
your_file.write("%s\n" % url)
your_file.close() #dont forget close it :)
the file open mode is wrong,'w' mode make this file was overwritten every time you open it,not appended to it. replace it to 'a' mode.
you can see this https://stackoverflow.com/a/23566951/8178794 for more detail
Open a file with option w overwrite the file if existring, use the mode a to append data to an existing file.
Try :
import requests
from os.path import splitext
# use mode='a' to append result without erasing filename
def dlink(url, filename, mode='w'):
r = requests.get(url)
if r.status_code != 200:
return
# here the link is valid
with open(filename, mode) as desc:
desc.write(url)
def dimg(img_url, img_name):
r = requests.get(img_url, stream=True)
if r.status_code != 200:
return
_, ext = splitext(img_url)
with open(img_name + ext, 'wb') as desc:
for chunk in r:
desc.write(chunk)
dlink('https://image.flaticon.com/teams/slug/freepik.jpg', 'links.txt')
dlink('https://image.flaticon.com/teams/slug/freepik.jpg', 'links.txt', 'a')
dimg('https://image.flaticon.com/teams/slug/freepik.jpg', 'freepik')
I'm building a little program that reads every line in a log file and if it finds a match it prints that line. The problem is, I have about 20 different log files and they all in the same folder. Is there a way I can parse through every single log file in a folder and print out the line that matches the searched word? Below is an example of what I have so far, but it prints nothing. The script needs to be able to incorporate readlines() and split()
What I have below doesn't work, but this is what I would expect it to look like. Any advice welcome.
def Preview():
path = ('C:Users/kev/Desktop/test/*.log')
files = glob.glob(path)
files.readlines()
for line in files:
if "test_word" in line:
print line
Preview()
This is how your code should look:
def Preview():
path = ('C:Users/kev/Desktop/test/*.log')
files = glob.glob(path)
for f in files:
f = open(f)
f = f.readlines():
for line in f:
if "test_word" in line:
print line
f.close()
Preview()
I have a for loop which creates a CSV of values of several files in a directory.
Within this loop I only want to create the file and write in the header once, currently I am doing this:
#name&path to table file
test = tablefile+"/"+str(cell[:-10])+"_Table.csv"
#write file
if not os.path.isfile(test):
csv.writer(open(test, "wt"))
with open(test, 'w') as output:
wr = csv.writer(output, lineterminator=',')
for val in header_note:
wr.writerow([val])
and to append data I have:
with open(test, 'a') as output:
wr = csv.writer(output, lineterminator=',')
for val in table_all:
wr.writerow([val])
Which works well, however, when I run the script over again another time it will append more data to the bottom of that same .csv. What I want is for the first time through the for-loop, is to just overwrite any existing .csv with a new one with a header then continue on appending data, and overwrite/re-write header once the script is run again. Thanks!
It look like you may have some code problems other than file handling, but here goes: You problem is basically that opening a file in 'w' mode will overwrite everything in the file, and opening in 'a' mode will not allow you to change the header line.
To get around this, you will have to get the contents of the file (if it already exists), then overwrite the file, including those lines that where there to begin with.
You will want something along the lines of:
if os.path.exists(file_name): # if file already exists
with open(file_name, 'r') as in_file: # open it
old_lines = in_file.readlines()[1:] # read all lines from file EXCEPT header line
with open(file_name, 'w') as out_file: # open file again, with 'w' to create/overwrite
out_file.write(new_header_line) # write new header line to file
for line in old_lines:
out_file.write(line) # write all preexisting lines back into file
# continue writing whatever you want.
I have an output file from a code which its name will ends to "_x.txt" and I want to connect two codes which second code will use this file as an input and will add more data into it. Finally, it will ends into "blabla_x_f.txt"
I am trying to work it out as below, but seems it is not correct and I could not solve it. Please help:
inf = str(raw_input(*+"_x.txt"))
with open(inf+'_x.txt') as fin, open(inf+'_x_f.txt','w') as fout:
....(other operations)
The main problem is that the "blabla" part of the file could change to any thing every time and will be random strings, so the code needs to be flexible and just search for whatever ends with "_x.txt".
Have a look at Python's glob module:
import glob
files = glob.glob('*_x.txt')
gives you a list of all files ending in _x.txt. Continue with
for path in files:
newpath = path[:-4] + '_f.txt'
with open(path) as in:
with open(newpath, 'w') as out:
# do something