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
  difference between forms of input a list to function akbarza 6 1,048 Feb-21-2024, 08:02 PM
Last Post: bterwijn
  cmath.rect accepts a negative modulus JMB 2 345 Jan-17-2024, 08:00 PM
Last Post: JMB
  Function parameter not writing to variable Karp 5 956 Aug-07-2023, 05:58 PM
Last Post: Karp
  File path by adding various variables Mishal0488 2 1,048 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 1,109 Dec-25-2022, 03:00 PM
Last Post: askfriends
  Script File Failure-Path Error? jerryf 13 3,478 Nov-30-2022, 09:58 AM
Last Post: jerryf
  Create a function for writing to SQL data to csv mg24 4 1,181 Oct-01-2022, 04:30 AM
Last Post: mg24
  Writing string to file results in one character per line RB76SFJPsJJDu3bMnwYM 4 1,396 Sep-27-2022, 01:38 PM
Last Post: buran
  Writing into 2 text files from the same function paul18fr 4 1,692 Jul-28-2022, 04:34 AM
Last Post: ndc85430
  Writing to json file ebolisa 1 1,017 Jul-17-2022, 04:51 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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