![]() |
How to get the file path and full file name - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: How to get the file path and full file name (/thread-18414.html) |
How to the file location - SriMekala - May-16-2019 Hi, I have a file name config_file_data302, I want to get the location of the file name to contain "congfig_file" in a given directory (avoid hidden file i.e, '.','..'). and write the same data in another file config_data303 in the same directory. RE: How to get the file path and full file name - heiner55 - May-17-2019 See here: https://docs.python.org/3/library/pathlib.html RE: How to get the file path and full file name - SriMekala - May-19-2019 Actually, I defined my path as: #define file name and write to webservicedata.csv import pandas as pd my_path='D:\pythonCodes\myproject' f=open('webservicedata.csv','w',newline='') f.write(data) f.close() #read partial data from above file and write into another file in the same director(absolute directory) df=pd.read_csv("D:\pythonCodes\myproject\webservicedata.csv") df1=df[:10] filename='config_file_data02' df1.to_csv(my_path,filename,index=False,header=False)I want to trace the full file name match the partial file name as "config_file" and its full path. RE: How to get the file path and full file name - perfringo - May-19-2019 I observe on row #3 my_path=='D:\pythonCodes\myproject' . I think that this is not intended this way because on row #11 you try to use my_path which will not exist.
RE: How to get the file path and full file name - SriMekala - May-19-2019 I already defined in row3, but won't it exists when it comes to row11? how to retain it if not exists? RE: How to get the file path and full file name - perfringo - May-19-2019 (May-19-2019, 11:32 AM)SriMekala Wrote: I already defined in row3 There is == so it is not assignment:>>> 2 == 2 True RE: How to get the file path and full file name - SriMekala - May-19-2019 Its typo error, sorry I did not notice that. Now corrected, Find a full file name from a partial filename in a given directory - SriMekala - May-19-2019 Hi, In a given directory there will always exist only one file contains the partial name"config_file", and I want to match which file name contains partial file name "config_file" and grab that full name of the file. Suppose, in a given directory: "D:\mainfolder" maybe there are "initial.csv" "final.csv" "config_file_data.csv" "data.csv" out of all CSV files, the file contains "config_file" is "config_file_data.csv", and I want to report this file name. Actually, periodically the files are updated, and but always there will be one file contain the name "config_file", and that I want to catch. RE: How to get the file path and full file name - perfringo - May-19-2019 It's unclear how code supposed to work, but there is glob: glob.glob('*config_file*') # returns list of matching filenames in current directory glob.glob('*config_file*')[0] # if you are sure that there is only one matching file glob.glob(r'D:\mainfolder\*config_file*') # should work, I am not using Windows, returns list of paths glob.glob(r'D:\**\*config_file*', recursive=True) # search recursively in D subfolders |