Python Forum
How to get the file path and full file name
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to get the file path and full file name
#1
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.
Reply
#2
See here:
https://docs.python.org/3/library/pathlib.html
Reply
#3
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.
Reply
#4
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'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#5
I already defined in row3, but won't it exists when it comes to row11? how to retain it if not exists?
Reply
#6
(May-19-2019, 11:32 AM)SriMekala Wrote: I already defined in row3

There is == so it is not assignment:

>>> 2 == 2
True
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#7
Its typo error, sorry I did not notice that. Now corrected,
Reply
#8
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.
Reply
#9
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
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  file open "file not found error" shanoger 8 1,133 Dec-14-2023, 08:03 AM
Last Post: shanoger
  Need to replace a string with a file (HTML file) tester_V 1 772 Aug-30-2023, 03:42 AM
Last Post: Larz60+
  How can I change the uuid name of a file to his original file? MaddoxMB 2 933 Jul-17-2023, 10:15 PM
Last Post: Pedroski55
  File path by adding various variables Mishal0488 2 1,039 Apr-28-2023, 07:17 PM
Last Post: deanhystad
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,112 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 1,128 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  Script File Failure-Path Error? jerryf 13 3,471 Nov-30-2022, 09:58 AM
Last Post: jerryf
Photo Making Zip file of a file and Directory Nasir 2 1,035 Oct-07-2022, 02:01 PM
Last Post: Nasir
  Need Help: Convert .pcl file to .pdf file ManuRaval 6 2,556 Sep-13-2022, 01:31 PM
Last Post: ManuRaval
  filter every 24 days file (considering file name) RolanRoll 5 2,231 Jun-23-2022, 11:41 AM
Last Post: RolanRoll

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020