I want to know why the loop wont stop hereHow can i make the attached pyunit python script search for tests in subfoldersWriting to file limitation on line lengthXLRD/Python: Encrypte Excel or make exceptionPython misinterpreting sys.argv[1] when called with * as filename?FileNotFoundError, os.getcwd() returns file name not a directoryReturning value returns NoneValue not displaying when called by functionCan't append a set of values from a ClassFor loop not stopping at the correct value in listPython script to find file in directory and create directory
How can "научись" mean "take it and keep trying"?
How did old MS-DOS games utilize various graphic cards?
Share calendar details request from manager's manager
How to construct an hbox with negative height?
What do abbreviations in movie scripts stand for?
Why didn't Voldemort recognize that Dumbledore was affected by his curse?
How to tell your grandparent to not come to fetch you with their car?
Are there downsides to using std::string as a buffer?
Can you see exclusive car models from other platforms when playing cross-platform?
Does Disney no longer produce hand-drawn cartoon films?
How to return a security deposit to a tenant
Overlapping String-Blocks
Are there any instruments that don't produce overtones?
C++ Arduino IDE receiving garbled `char` from function
Is it possible to 'live off the sea'
Why did the Herschel Space Telescope need helium coolant?
What is the `some` keyword in SwiftUI?
How to hide an urban landmark?
How to handle self harm scars on the arm in work environment?
Is using haveibeenpwned to validate password strength rational?
PhD - Well known professor or well known school?
Why would future John risk sending back a T-800 to save his younger self?
What makes Ada the language of choice for the ISS's safety-critical systems?
Is open-sourcing the code of a webapp not recommended?
I want to know why the loop wont stop here
How can i make the attached pyunit python script search for tests in subfoldersWriting to file limitation on line lengthXLRD/Python: Encrypte Excel or make exceptionPython misinterpreting sys.argv[1] when called with * as filename?FileNotFoundError, os.getcwd() returns file name not a directoryReturning value returns NoneValue not displaying when called by functionCan't append a set of values from a ClassFor loop not stopping at the correct value in listPython script to find file in directory and create directory
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I was trying to write a program that searches a nested directory and then returns a location of searched file.In order to nest the scan inside directories I called the function inside the function. I was successful in doing so as the program worked but when I wanted was to stop the searching when the first hit is done. The code is below. Can someone make me understand why he iteration won't stop.
search = sys.argv[2]
def find(loca,term):
count = 1
for file in os.listdir(loca):
if (count > 1):
break
try:
os.chdir(f'locafile')
newloca = loca + '\' + file
find(newloca,term)
except NotADirectoryError:
pass
except PermissionError:
pass
if(file == term):
print(file)
print(f"found the term at")
print(os.getcwd())
count += 1
find("E:/",search)
python python-3.x
add a comment |
I was trying to write a program that searches a nested directory and then returns a location of searched file.In order to nest the scan inside directories I called the function inside the function. I was successful in doing so as the program worked but when I wanted was to stop the searching when the first hit is done. The code is below. Can someone make me understand why he iteration won't stop.
search = sys.argv[2]
def find(loca,term):
count = 1
for file in os.listdir(loca):
if (count > 1):
break
try:
os.chdir(f'locafile')
newloca = loca + '\' + file
find(newloca,term)
except NotADirectoryError:
pass
except PermissionError:
pass
if(file == term):
print(file)
print(f"found the term at")
print(os.getcwd())
count += 1
find("E:/",search)
python python-3.x
1
Please share the output of this program.
– Artemis Fowl
Mar 24 at 17:22
When you stepped through your program using a debugger, what did you see? Was it anything unexpected?
– Roland Illig
Mar 24 at 17:22
the output of this program will be as I told upside. The program scans the entire E directory and returns the search term which is provided in the console. All I want to know is why wont the iteration stop. Or am I doing something wrong
– Dhiraz Gazurel
Mar 24 at 17:25
No the program works fine. Only the loop is not stopping
– Dhiraz Gazurel
Mar 24 at 17:26
add a comment |
I was trying to write a program that searches a nested directory and then returns a location of searched file.In order to nest the scan inside directories I called the function inside the function. I was successful in doing so as the program worked but when I wanted was to stop the searching when the first hit is done. The code is below. Can someone make me understand why he iteration won't stop.
search = sys.argv[2]
def find(loca,term):
count = 1
for file in os.listdir(loca):
if (count > 1):
break
try:
os.chdir(f'locafile')
newloca = loca + '\' + file
find(newloca,term)
except NotADirectoryError:
pass
except PermissionError:
pass
if(file == term):
print(file)
print(f"found the term at")
print(os.getcwd())
count += 1
find("E:/",search)
python python-3.x
I was trying to write a program that searches a nested directory and then returns a location of searched file.In order to nest the scan inside directories I called the function inside the function. I was successful in doing so as the program worked but when I wanted was to stop the searching when the first hit is done. The code is below. Can someone make me understand why he iteration won't stop.
search = sys.argv[2]
def find(loca,term):
count = 1
for file in os.listdir(loca):
if (count > 1):
break
try:
os.chdir(f'locafile')
newloca = loca + '\' + file
find(newloca,term)
except NotADirectoryError:
pass
except PermissionError:
pass
if(file == term):
print(file)
print(f"found the term at")
print(os.getcwd())
count += 1
find("E:/",search)
python python-3.x
python python-3.x
asked Mar 24 at 17:18
Dhiraz GazurelDhiraz Gazurel
59110
59110
1
Please share the output of this program.
– Artemis Fowl
Mar 24 at 17:22
When you stepped through your program using a debugger, what did you see? Was it anything unexpected?
– Roland Illig
Mar 24 at 17:22
the output of this program will be as I told upside. The program scans the entire E directory and returns the search term which is provided in the console. All I want to know is why wont the iteration stop. Or am I doing something wrong
– Dhiraz Gazurel
Mar 24 at 17:25
No the program works fine. Only the loop is not stopping
– Dhiraz Gazurel
Mar 24 at 17:26
add a comment |
1
Please share the output of this program.
– Artemis Fowl
Mar 24 at 17:22
When you stepped through your program using a debugger, what did you see? Was it anything unexpected?
– Roland Illig
Mar 24 at 17:22
the output of this program will be as I told upside. The program scans the entire E directory and returns the search term which is provided in the console. All I want to know is why wont the iteration stop. Or am I doing something wrong
– Dhiraz Gazurel
Mar 24 at 17:25
No the program works fine. Only the loop is not stopping
– Dhiraz Gazurel
Mar 24 at 17:26
1
1
Please share the output of this program.
– Artemis Fowl
Mar 24 at 17:22
Please share the output of this program.
– Artemis Fowl
Mar 24 at 17:22
When you stepped through your program using a debugger, what did you see? Was it anything unexpected?
– Roland Illig
Mar 24 at 17:22
When you stepped through your program using a debugger, what did you see? Was it anything unexpected?
– Roland Illig
Mar 24 at 17:22
the output of this program will be as I told upside. The program scans the entire E directory and returns the search term which is provided in the console. All I want to know is why wont the iteration stop. Or am I doing something wrong
– Dhiraz Gazurel
Mar 24 at 17:25
the output of this program will be as I told upside. The program scans the entire E directory and returns the search term which is provided in the console. All I want to know is why wont the iteration stop. Or am I doing something wrong
– Dhiraz Gazurel
Mar 24 at 17:25
No the program works fine. Only the loop is not stopping
– Dhiraz Gazurel
Mar 24 at 17:26
No the program works fine. Only the loop is not stopping
– Dhiraz Gazurel
Mar 24 at 17:26
add a comment |
1 Answer
1
active
oldest
votes
Since you're using recursion, you need some way of signaling the parent when you find a hit. You can use return
for that.
def find(loca,term):
count = 1
for file in os.listdir(loca):
if (count > 1):
break
try:
os.chdir(f'locafile')
newloca = loca + '\' + file
if find(newloca,term):
return 1
except NotADirectoryError:
pass
except PermissionError:
pass
if(file == term):
print(file)
print(f"found the term at")
print(os.getcwd())
count += 1
return 1
thnx your code works.. one more thing is this the efficient method to do this or there is something else more efficient
– Dhiraz Gazurel
Mar 24 at 18:06
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55326420%2fi-want-to-know-why-the-loop-wont-stop-here%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Since you're using recursion, you need some way of signaling the parent when you find a hit. You can use return
for that.
def find(loca,term):
count = 1
for file in os.listdir(loca):
if (count > 1):
break
try:
os.chdir(f'locafile')
newloca = loca + '\' + file
if find(newloca,term):
return 1
except NotADirectoryError:
pass
except PermissionError:
pass
if(file == term):
print(file)
print(f"found the term at")
print(os.getcwd())
count += 1
return 1
thnx your code works.. one more thing is this the efficient method to do this or there is something else more efficient
– Dhiraz Gazurel
Mar 24 at 18:06
add a comment |
Since you're using recursion, you need some way of signaling the parent when you find a hit. You can use return
for that.
def find(loca,term):
count = 1
for file in os.listdir(loca):
if (count > 1):
break
try:
os.chdir(f'locafile')
newloca = loca + '\' + file
if find(newloca,term):
return 1
except NotADirectoryError:
pass
except PermissionError:
pass
if(file == term):
print(file)
print(f"found the term at")
print(os.getcwd())
count += 1
return 1
thnx your code works.. one more thing is this the efficient method to do this or there is something else more efficient
– Dhiraz Gazurel
Mar 24 at 18:06
add a comment |
Since you're using recursion, you need some way of signaling the parent when you find a hit. You can use return
for that.
def find(loca,term):
count = 1
for file in os.listdir(loca):
if (count > 1):
break
try:
os.chdir(f'locafile')
newloca = loca + '\' + file
if find(newloca,term):
return 1
except NotADirectoryError:
pass
except PermissionError:
pass
if(file == term):
print(file)
print(f"found the term at")
print(os.getcwd())
count += 1
return 1
Since you're using recursion, you need some way of signaling the parent when you find a hit. You can use return
for that.
def find(loca,term):
count = 1
for file in os.listdir(loca):
if (count > 1):
break
try:
os.chdir(f'locafile')
newloca = loca + '\' + file
if find(newloca,term):
return 1
except NotADirectoryError:
pass
except PermissionError:
pass
if(file == term):
print(file)
print(f"found the term at")
print(os.getcwd())
count += 1
return 1
answered Mar 24 at 17:42
John GordonJohn Gordon
11.9k51833
11.9k51833
thnx your code works.. one more thing is this the efficient method to do this or there is something else more efficient
– Dhiraz Gazurel
Mar 24 at 18:06
add a comment |
thnx your code works.. one more thing is this the efficient method to do this or there is something else more efficient
– Dhiraz Gazurel
Mar 24 at 18:06
thnx your code works.. one more thing is this the efficient method to do this or there is something else more efficient
– Dhiraz Gazurel
Mar 24 at 18:06
thnx your code works.. one more thing is this the efficient method to do this or there is something else more efficient
– Dhiraz Gazurel
Mar 24 at 18:06
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55326420%2fi-want-to-know-why-the-loop-wont-stop-here%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Please share the output of this program.
– Artemis Fowl
Mar 24 at 17:22
When you stepped through your program using a debugger, what did you see? Was it anything unexpected?
– Roland Illig
Mar 24 at 17:22
the output of this program will be as I told upside. The program scans the entire E directory and returns the search term which is provided in the console. All I want to know is why wont the iteration stop. Or am I doing something wrong
– Dhiraz Gazurel
Mar 24 at 17:25
No the program works fine. Only the loop is not stopping
– Dhiraz Gazurel
Mar 24 at 17:26