Open files in directory python


















Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Open file in a relative location in Python Ask Question. Asked 10 years, 4 months ago. Active 30 days ago. Viewed k times. I found that below simple code will work.. Improve this question. Neuron 4, 4 4 gold badges 27 27 silver badges 48 48 bronze badges. You're using unescaped backslashes. That's one disadvantage.

Several disadvantages. Your string won't work. Also, 2 you are using getcwd which is the path you were in when you execute the script. I thought you wanted relative to the script location but now am wondering.

And 3 , always use os. Your path joining line should be os. And for good measure See here for the best explanation of with statements I've seen. Add a comment. Active Oldest Votes. Improve this answer. Russ Russ 9, 11 11 gold badges 39 39 silver badges 54 54 bronze badges. Arash The disadvantage to that is the cwd current working directory can be anything, and won't necessarily be where your script is located. Soumendra I think you are trying it in the console.

Improve Article. Save Article. Like Article. Last Updated : 03 Jun, Get the list of all files and directories. Prints only text file present in My Folder. DirEntry objects. List all files and directories in the specified path. It returns an iterator which will. Stack Overflow for Teams — Collaborate and share knowledge with a private group.

Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. How to reliably open a file in the same directory as the currently running script Ask Question. Asked 11 years, 2 months ago. Active 3 months ago. Viewed k times. I used to open files that were in the same directory as the currently running Python script by simply using a command like: open "Some file.

Since then I've used a command of the form open os. Here's what I've been able to figure out so far: os. Edit: I just realized that what I want to do would be better described as "open a file in the same directory as the containing module". Improve this question. Double-click or no, the script runs in your current working directory, not the directory where you saved the script.

Add a comment. Active Oldest Votes. Improve this answer. Hubert Grzeskowiak The rest should work as expected. The problem with this is it will vary if you the file being run is from the interrupter directly or if it is imported. See my answer for the differences between file and sys. There is no need to use getcwd , because the os.

Add a comment. Active Oldest Votes. Yevgen Yampolskiy 6, 3 3 gold badges 24 24 silver badges 23 23 bronze badges. In case you're not in the specified directory i.

The file might also already be in use. Some other app might have opened it and not closed it. Thomas Orozco Thomas Orozco Deepak Yadav Deepak Yadav 6 6 silver badges 13 13 bronze badges. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog.



0コメント

  • 1000 / 1000