Idk if I'm overthinking this or just really tired, but I was trying to figure out the best way to add text to this function
print(os.path.getsize(os.path.join('/Users/raelynsade/Documents/cpt180stuff/pets/dogs', 'dogs.jpg')), 'bytes')
this is the results
59520 bytes
but I need
dogs.jpg: 5920 bytes
First, get the size in bytes aond convert it to a string:
path_name = '/Users/raelynsade/Documents/cpt180stuff/pets/dogs'
file_name = 'dogs.jpg'
size_string = str(os.path.getsize(os.path.join(path_name, file_name)))
Then concatenate it with the desired text:
output_text = file_name + ': ' + size_string + ' bytes'
print(output_text)
Alternatively, you could use an f-string instead, with the same result:
output_text = f"{file_name}: {size_string} bytes"
print(output_text)
You could do everything on one or two lines like the other answers, but it's always better to be clearer rather than compact, there's no need to sacrifice readability to make code as small as possible:
file_name = "dogs.jpg"
print(f"{file_name}: {str(os.path.getsize(os.path.join('/Users/raelynsade/Documents/cpt180stuff/pets/dogs', file_name)))} bytes")
Maybe I am being dumb but can you do this:
print('dogs jpg:', os.path.getsize(os.path.join('/Users/raelynsade/Documents/cpt180stuff/pets/dogs', 'dogs.jpg')), 'bytes')
Related
With this python's code I may read all tickers in the tickers.txt file:
fh = open("tickers.txt")
tickers_list = fh.read()
print(tickers_list)
The output that I obtain is this:
A2A.MI, AMP.MI, ATL.MI, AZM.MI, BGN.MI, BMED.MI, BAMI.MI,
Neverthless, I'd like to obtain as ouput a ticker string exactly formatted in this manner:
["A2A.MI", "AMP.MI", "ATL.MI", "AZM.MI", ...]
Any idea?
Thanks in advance.
If you want the output to look in that format you want, you would need to do the following:
tickers_list= "A2A.MI, AMP.MI, ATL.MI, AZM.MI, BGN.MI, BMED.MI, BAMI.MI"
print("["+"".join(['"' + s + '",' for s in tickers_list.split(",")])[:-1]+"]")
With the output:
["A2A.MI"," AMP.MI"," ATL.MI"," AZM.MI"," BGN.MI"," BMED.MI"," BAMI.MI"]
Code explanation:
['"' + s + '",' for s in tickers_list.split(",")]
Creates a list of strings that contain each individual value, with the brackets as well as the comma.
"".join(...)[:-1]
Joins the list of strings into one string, removing the last character which is the extra comma
"["+..+"]"
adds the closing brackets
Another alternative is to simple use:
print(tickers_list.split(","))
However, the output will be slightly different as in:
['A2A.MI', ' AMP.MI', ' ATL.MI', ' AZM.MI', ' BGN.MI', ' BMED.MI', ' BAMI.MI']
Having ' instead of "
A solution for that however is this:
z = str(tickers_list.split(","))
z = z.replace("'",'"')
print(z)
Having the correct output, by replacing that character
you can to use Split function:
tickers_list = fh.read().split(',')
I'm trying to filter some log files that are in the format of a table/dataset but .endswith() and .startswith() are not meeting my requirments. I'm using an anonymous function but need to adapt my Python code to check if a string contains .jpg
logfilejpg = sc.textFile("/loudacre/logs/*.log").filter(lambda line: line.endswith('.jpg'))
Use in:
'.jpg' in 'something.jpg foo'
Out: True
You can also put it in your lambda expression:
lambda line: '.jpg' in line
Example:
list(filter(lambda line: '.jpg' in line, ["foo", "foo.jpg.bar", "bar.jpg"]))
Out: ['foo.jpg.bar', 'bar.jpg']
To get the index of where the ".jpg" starts at:
hello = "world.jpg"
print(hello.find(".jpg"))
You can split the inintial string by " " (space) then by "." and take the second value in the resulting array. Of course it depends on how your initial string is. The basic idea is you can isolate the ".jpg" and use equal to check.
To verify that the file is actually a jog, you can try to open it. If it fails, the file is ether other format or corrupt, see also the excepption you get.
Using str.find() and len(), you could find the substring like so:
a_string = 'there is a .jpg here.'
start = a_string.find('.jpg') # The lowest index in a_string where '.jpg' is found
end = start + len('.jpg')
print(a_string[start:end])
# .jpg
I used the following line to rename my file by adding timing and remove extra space and replace it with (-)
if i would like to add extra information like lable before the timing ,
filename = ("%s_%s.mp4" %(pfile, time.strftime("%Y-%m-%d_%H:%M:%S",time.localtime()))).replace(" ", "-")
the current output looks like
testfile_2016-07-25_12:17:14.mp4
im looking to have the file output as
testfile_2016-07-25_12:17:14-MediaFile.mp4
try the following ,
filename = ("%s_%s_%s.mp4" %(pfile, time.strftime("%Y-%m-%d_%H:%M:%S","Mediafile",time.localtime()))).replace(" ", "-")
what did i missed here ?
You're using the function strftime incorrectly. Strftime only takes 2 arguments and you're passing it 3.
You would need to generate the string from the time and apply some string operations to append the extra info.
If you want to add MediaFile to the end of the filename simply do something like this.
filename = ("%s_%s-MediaFile.mp4" %(pfile, time.strftime("%Y-%m-%d_%H:%M:%S",time.localtime()))).replace(" ", "-")
filename = ("%s_%s-%s.mp4" %(pfile, time.strftime("%Y-%m-%d_%H:%M:%S",time.localtime()), 'MediaFile')).replace(' ', '-')
# 'testfile_2016-07-25_10:29:28-MediaFile.mp4'
To understand better how this works and slightly improve readability, you can define your time stamp in a separate variable:
timestr = time.strftime("%Y-%m-%d_%H:%M:%S", time.localtime()) # 2016-07-25_10:31:03
filename = ("%s_%s-%s" %(pfile, timestr, 'MediaFile')).replace(' ', '-')
# 'testfile_2016-07-25_10:31:03-MediaFile.mp4'
or
filename = ("%s_%s-MediaFile.mp4" %(pfile, timestr)).replace(' ', '-')
For completeness, you can also use the format() method:
filename = '{0}_{1}-MediaFile.mp4'.format(pfile, timestr).replace(' ', '-')
What you are looking for should be :
filename = ("%s_%s_%s.mp4" %(pfile, time.strftime("%Y-%m-%d_%H:%M:%S",time.localtime()),"Mediafile")).replace(" ", "-")
In your original code, the 'Mediafile' string was not in the right place : you put it as an argument of strftime(), when you should put it as one of the string to replace, in the 2nd level of parentheses.
I'm taking out all of the coordinates from a kml file. This works, but my issue is that at the end of my file I end up with "}, }" instead of a "}}". I realize I can just manually edit the end of the file after I make it, but I'd rather have that be automatically done in the code. The commented-out section contains the code that I found in another answer, but it doesn't do anything for me.
import re
import os
KML = open('NYC_Tri-State_Area.kml','r')
NYC_Coords = open('NYC_Coords.txt', 'w')
coords = re.findall(r'((?<=<coordinates>).*(?=<\/coordinates>))', KML.read())
NYC_Coords.write("{")
for coord in coords:
NYC_Coords.write("{" + str(coord) + "}, ")
...
with open('NYC_Coords.txt', 'rb+') as filehandle:
filehandle.seek(-2, os.SEEK_END)
filehandle.truncate()
...
NYC_Coords.write("}")
KML.close()
NYC_Coords.close()
There are a number of suggestions for fixing your problem. First, it's probably a bad idea to use a regex to parse XML-derived documents. There are many dedicated modules for parsing KML, like pyKML
Second, you can eliminate the need to truncate completely by correctly generating your string. In this case, by replacing:
for coord in coords:
NYC_Coords.write("{" + str(coord) + "}, ")
with the very simple one-liner:
NYC_Coords.write(', '.join('{{{}}}'.format(coord) for coord in coords))
You will now no longer have an extra trailing ', ' at the end of your document.
for coord in coords:
NYC_Coords.write("{" + str(coord) + "}, ")
Here, you write ", " at the end of every coord. But what you really want to do is write ", " between each coord. join can be used to interleave strings in this way.
NYC_Coords.write(", ".join("{" + str(coord) + "}" for coord in coords))
Now you will have no trailing comma at the end of your final coord.
As coords is a list of strings, you could do:
NYC_Coords.write("{{{{{0}}}}}".format("}, {".join(coords)))
Unfortunately, your output uses the same syntax as str.format, so you need to escape a lot of curly braces in the template... Demo:
>>> coords = ["foo", "bar", "baz"]
>>> "{{{{{0}}}}}".format("}, {".join(coords))
'{{foo}, {bar}, {baz}}'
You could avoid the escapes with C-style string formatting:
>>> "{{%s}}" % "}, {".join(coords)
'{{foo}, {bar}, {baz}}'
I am converting a command line to a python string. The command line is:
../src/clus -INFILE=../input/tua40.sq -OUTPUT=OUT
The python statement is:
c_dir = '~/prj/clus/'
c_bin = c_dir + 'src/clus'
c_data = c_dir + 'input/tua40.sq'
c = LiveProcess()
c.executable = c_bin
c.cwd = c_dir
c.cmd = [c.executable] + ['-INFILE=', 'c_data, '-OUTPUT=OUT']
Problem is the c.cmd at the end looks like
~/prj/clus/src/clus -INFILE= ~/prj/clus/input/tua40.sq ...
Not that there is a 'space' after '=' which causes the program to report an error.
How can I concatenate '=' to the path?
LiveProcess is expecting an argv-style list of arguments. Where you want to make one argument, you need to provide one string. So use concatenation to make the string:
c.cmd = [c.executable] + ['-INFILE='+c_data, '-OUTPUT=OUT']
Also, no need for the list addition:
c.cmd = [c.executable, '-INFILE='+c_data, '-OUTPUT=OUT']
Why don't you just concatenate string like this:
a = 'A'+'B'
then
a == 'AB'
that is in your example
['-INFILE=' + c_data, '-OUTPUT=OUT']
Given that it looks like you're concatenating paths, you should be using os.path.join, not regular string concat.
Try this:
c.cmd = [c.executable] + ['-INFILE='+c_data, '-OUTPUT=OUT']