Hello I have a piece of code where I want to join a path with a picture name which I am getting from an excel file. The thing is that my code changes my path string.
src=os.path.join('C:\\Users\\ekrem\\Desktop\\Distributions\\16-17',ws.cell(row=cell.row,column=2).value)
print('src=',src)
so here my src variable should be something like this:
C:\Users\ekrem\Desktop\Distributions\16-17\26231043_1686575684735993_7548330554586169888_n.jpg
but as you see I printed the src variable and I got this
src= C:\Users\ekrem\Desktop\16-17\26231043_1686575684735993_7548330554586169888_n.jpg
here the distribution folder is missing, have you any idea why?
Thank you in advance
In this case assuming the end piece is always a single filename it might be easier to do this with f-strings. That way there is no path weirdness that happens something like this should work (Assuming you're on python 3.6+, if you aren't then do this instead):
src = f'C:\\Users\\ekrem\\Desktop\\Distributions\\16-17\\{ws.cell(row=cell.row,column=2).value}'
print(f'{source=}') # Print source with label
P.S I would recommend switching the C:\\Users\\<name> with %USERPROFILE% (so %USERPROFILE%\\Desktop\\Distributions\\16-17\\), that way if you change accounts or hard-drives this still works.
Then just do validation with:
if os.path.exists(src):
# Do stuff
else:
raise FileNotFoundError(f"{src} file could not be found")
Related
Greetings I Have Folder named '10k' which contains images named 1_left 1_right as shown below.
My python code to print names of file in Folder:
main_file = '10k'
path = os.path.join(main_file,'*g')
files = glob.glob(path)
#l='10k\10_left.jpeg'
for f1 in files:
#print(os.path.basename(f1))
fstr=str(f1)
print(fstr)
The output is weird when I Print
it does not the desired names
Output :
Please guide me.
✓ #vidit02100, the problem is really very interesting. As I understood from your code, you want to only print the name of image files present inside 10k directory.
In image, you have not commented the lines which you have commented in the problem's code.
If you will show full code and tell about the number of images inside 10k directory then it would be much better for me to help you.
May be your code will be printing the images in reverse order if there will be +10000 images inside 10k. Please check and let me know.
✓ As I know, jpg, jpeg & png are the most popular image file extensions that ends with g.
✓ So place all these extensions in one list and use another for loop to iterate over it and place your code inside it.
Please comment if my suggestion doesn't satisfy your need. I will update my answer based on your provided inputs and outputs.
✓ Here is your modified code.
main_file = '10k'
file_formats = ["png", "jpg", "jpeg"]
for file_format in file_formats:
path = os.path.join(main_file, '*.' + file_format )
files = glob.glob(path)
for f1 in files:
fstr = str(f1)
print(fstr)
Without more information I can only guess what you wanted this to print.
If your desired output is first 1_left then 1_right and so on like it is presented in your folder, the reason for that is that python sorts the files in a different way than you OS does.
As far as I can tell, files is just a list. So you could sort it yourself using sort and a custom key, like files.sort(key=lambda x: int(x.split("_")[0])).
This will sort the list by the number at the beginning, and numbers other than strings are sorted like you'd probably expect (so first 1, then 2 and so on).
I would like to perform a sort of "manual" batch operation where Python looks in a directory, sees a list of files, then automatically displays them one at a time and waits for user input before moving on to the next file. I am going to assume the files have relatively random names (and the order in which Python chooses to display them doesn't really matter).
So, I might have pic001.jpg and myCalendar.docx. Is there a way to have Python move through these (in any order) so that I can prepend something to each one manually? For instance, it could look like
Please type a prefix for each of the following:
myCalendar.docx:
and when I typed "2014" the file would become 2014_myCalendar.docx. Python would then go on to say
Please type a prefix for each of the following:
myCalendar.docx: 2014
... myCalendar.docx renamed to 2014_myCalendar.docx
pic001.jpg:
then I could make it disneyland_pic001.jpg.
I know how to rename files, navigate directories, etc. I'm just not sure how to get Python to cycle through every file in a certain directory, one at a time, and let me modify each one. I think this would be really easy to do with a for loop if each of the files was numbered, but for what I'm trying to do, I can't assume that they will be.
Thank you in advance.
Additionally, if you could point me to some tutorials or documentation that might help me with this, I'd appreciate that as well. I've got http://docs.python.org open in a few tabs, but as someone who's relatively new to Python, and programming in general, I find their language to be a little over my head sometimes.
Something like this (untested):
DIR = '/Volumes/foobar'
prefix = raw_input('Please type a prefix for each of the following: ')
for f in os.listdir(DIR):
path = os.path.join(DIR, f)
new_path = os.path.join(DIR, '%s%s' % (prefix, f))
try:
os.rename(path, new_path)
print 'renamed', f
except:
raise
I am trying to launch one application through python. I am having problem with this can any give me a solution?
path1= "C:\\Program Files (x86)\\XYZ\\NX2\\RT900"
ver="7.50 Internal Release"
path2="bin\\Rt900.exe"
path3=os.path.join(path1,ver)
path4=os.path.join(path3,path2)
App.open("path4")
Can anyone tell me what's wrong in above statement?
you should join path1 with path2 not ver
path3 = os.path.join(path1, path2)
also you are doing path4=os.path.join(path3,path4) here path4 is used before assignment
As avasal has mentioned you should join paths correctly:
path3 = os.path.join(path1, path2)
It also looks like you need to open the path like so:
App.open(path3)
Note the lack of quotation marks in the last line.
Edit:
Seeing you keep changing the code in your question I have noted another error in your code:
path4=os.path.join(path3,path4)
This line will always file you are trying to join with a variable that doesn't even exist yet. A variable can't reference itself when it is being assigned for the first time. In this case you are doing os.path.join(path3, path4) but path4 doesn't even exist yet!
I have a directory structure as below
/home/damon/dev/python/misc/path/
/project/mycode.py
/app/templates/
I need to get the absolute path of the templates folder from mycode.py
I tried to write mycode.py as
import os
if __name__=='__main__':
PRJ_FLDR=os.path.dirname(os.path.abspath(__file__))
print 'PRJ_FLDR=',PRJ_FLDR
apptemplates = os.path.join(PRJ_FLDR,'../app/templates')
print 'apptemplates=',apptemplates
I expected the apptemplates to be
/home/damon/dev/python/misc/path/app/templates
but I am getting
/home/damon/dev/python/misc/path/project/../app/templates
How do I get the correct path?
That path is correct, try it. But if you want to remove the redundant 'project/../' section for clarity, use os.path.normpath
os.path.normpath(path)
Normalize a pathname. This collapses redundant separators and up-level references so that A//B, A/B/, A/./B and A/foo/../B all become A/B.
http://docs.python.org/2/library/os.path.html#os.path.normpath
Is this what you want?
import os
if __name__=='__main__':
PRJ_FLDR=os.path.dirname(os.path.abspath(__file__))
print 'PRJ_FLDR=',PRJ_FLDR
apptemplates = os.path.abspath(os.path.join(PRJ_FLDR, '../app/templates'))
print 'apptemplates=',apptemplates
Considering the comments, I made the proper edit.
I tried this ,and it seems to work
parentpath=os.path.abspath(os.path.join(os.path.dirname(__file__),".."))
apptemplates=os.path.join(parentpath,'app/templates')
This works:
apptemplates = os.path.join(os.path.split(PRJ_FLDR)[0], "app/templates")
This probably has an embarrassingly easy answer, but I'm not sure what it is.
In my python code, there is a part where I want to save an array (called "stokes_columns" which is just full of floats) into a text file.
I did this fine with the following:
np.savetxt('../all_pulsars_1400list/%s_1400list.txt' % pname,stokes_columns, delimiter='\t')
The error message I get says:
no such file or directory: '~/all_pulsars_1400list/J0543_1400list.txt'
Where J0543 is the first variable to be used for the '%s'
but - I don't understand because of course there is no file called that - that is the file I am trying to create.
I've double checked the path and it exists.
Is there something obvious I'm doing wrong? Thank you.
You need to expand path to absolute path like this:
>>> import os
>>> os.path.expanduser('~/all_pulsars_1400list/J0543_1400list.txt')
'home/xxx/all_pulsars_1400list/J0543_1400list.txt'