473,320 Members | 1,732 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

how to search if a file is present in a folder or not

Hi!
Can any1 help me out.
Actually i wanted to search if a file is present in a folder using python script.
The file is under creation and i want to keep on searching in that folder if the file has been successfully created or not.please help me!!!
Dec 15 '06 #1
3 1456
bvdet
2,851 Expert Mod 2GB
Hi!
Can any1 help me out.
Actually i wanted to search if a file is present in a folder using python script.
The file is under creation and i want to keep on searching in that folder if the file has been successfully created or not.please help me!!!
Change the drive, directories and file name to match your case and try this:
Expand|Select|Wrap|Line Numbers
  1. import os
  2.  
  3. def fileChk(file_name):
  4.     try:
  5.         f = open(file_name, "r")
  6.         f.close()
  7.         return True
  8.     except IOError, e:
  9.         # unable to open file
  10.         return False
  11.  
  12. dir_name = (os.path.join('C:\\', 'SDS2_7.0', 'macro'))
  13. file_name = ("your_file.dat")
  14. if fileChk(os.path.join(dir_name, file_name)):
  15.     print "Success!"
  16. else:
  17.     print "File does not exist"
Dec 15 '06 #2
ghostdog74
511 Expert 256MB
check out os.path.exists() . It should do what you want.
Dec 15 '06 #3
bvdet
2,851 Expert Mod 2GB
check out os.path.exists() . It should do what you want.
Good point ghostdog74. Thanks!
Expand|Select|Wrap|Line Numbers
  1. if os.path.exists(os.path.join(dir_name, file_name)):
  2.     print "Success!"
  3. else:
  4.     print "File does not exist"
Dec 15 '06 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Les Juby | last post by:
A year or two back I needed a search script to scan thru HTML files on a client site. Usual sorta thing. A quick search turned up a neat script that provided great search results. It was fast,...
0
by: mm | last post by:
Can someone with Win 2K please help me here. Best yet, can some MVP duplicate it and report it to MS. I can't find any reference to this issue on the Web, or in MS Knowledge Base. I have been...
5
by: tmb | last post by:
I need to search a folder & sub-folders for key words in ASP files... I can open the files with Notepad and see the text string there... But when I try to navigate to the folder with Windows...
13
by: Leo | last post by:
Hi I had asekd previously a question qith regards to running a DLL file from ASP. It was not running for me. Bo error returned, nothing, Just an empty string (when it should have not been...
4
by: Richard | last post by:
Hi all, Is there any class in .NET 1.x that will help me set file & folder permissions for a user? Aka - I would like to do the in code equivalent of right clicking a folder in file explorer...
2
by: Mark Rae | last post by:
Hi, Just looking for some advice and/or recommendations here. Scenario is corporate private intranet, ASP.NET 2 on Win2K3S, SQL Server 2005, WinXP + IE6 on all client desktops. Client has a...
9
by: Raj | last post by:
Following is a code to open a file & print its contents on screen. But im getting the message "file can't be opened". The thing is, that if i create a file separately in a particular folder in C...
0
by: =?Utf-8?B?QnJ5YW4=?= | last post by:
Hello group. I've migrated from Win 2003 server to Win 2008 server. I've been banging my head agaist a wall for several days now trying to figure this out. I have the following script that will...
1
by: veer | last post by:
hi it looks a silly question but i m getting confused actually i want to copy folders from one location to another like from C:\abc To D:\xyz here abc and xyz are two folders in c and d drive it...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.