Python Forum
Writing a function that accepts a path to an input file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Writing a function that accepts a path to an input file
#1
import os
path = 'c:\users\name\desktop\project\input_samples'

for xyz_file in os.listdir(path):
    if xyz_file.endswith('xyz'):
    (operations of function here)
In the folder 'input samples' I have multiple .xyz file (coordinate files). I'm trying to create a function that accepts a path to one input file and not the directory. It don't want it to loop over the files, just process a single file. I want to do it without having to remove all the files except 1 in the directory path.

Any help would be much appreciated :)
Reply
#2
add \yourfilename after existing path
Reply
#3
that results in an error for line 4

WindowsError: [Error 267] The directory name is invalid: 'c:\\users\\sammy\\desktop\\NanoNet\\input_samples\\SiNW2.xyz\\*.*'
Reply
#4
why add \\*.* at the end?
you claim you want single file. That is SiNW2.xyz, right? In this case just 'c:\\users\\sammy\\desktop\\NanoNet\\input_samples\\SiNW2.xyz' would do.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Did you know that map() accepts more than one iterable? DeaD_EyE 2 526 Apr-13-2025, 10:16 AM
Last Post: noisefloor
  Problems writing a large text file in python Vilius 4 1,052 Dec-21-2024, 09:20 AM
Last Post: Pedroski55
  Input function oldschool 1 747 Sep-14-2024, 01:02 PM
Last Post: deanhystad
  import a function from another file using relative path paul18fr 6 3,117 Aug-01-2024, 06:40 AM
Last Post: paul18fr
  writing list to csv file problem jacksfrustration 5 2,387 Jul-04-2024, 08:15 PM
Last Post: deanhystad
  difference between forms of input a list to function akbarza 6 2,381 Feb-21-2024, 08:02 PM
Last Post: bterwijn
  cmath.rect accepts a negative modulus JMB 2 1,254 Jan-17-2024, 08:00 PM
Last Post: JMB
  Function parameter not writing to variable Karp 5 2,187 Aug-07-2023, 05:58 PM
Last Post: Karp
  File path by adding various variables Mishal0488 2 3,965 Apr-28-2023, 07:17 PM
Last Post: deanhystad
Information How to take url in telegram bot user input and put it as an argument in a function? askfriends 0 2,370 Dec-25-2022, 03:00 PM
Last Post: askfriends

Forum Jump:

User Panel Messages

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