Python Forum

Full Version: How to get the file path and full file name
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.
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.
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.
I already defined in row3, but won't it exists when it comes to row11? how to retain it if not exists?
(May-19-2019, 11:32 AM)SriMekala Wrote: [ -> ]I already defined in row3

There is == so it is not assignment:

>>> 2 == 2
True
Its typo error, sorry I did not notice that. Now corrected,
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.
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