About 63 results
Open links in new tab
  1. python - Find the current directory and file's directory - Stack …

    To get the full path to the directory a Python file is contained in, write this in that file: import os dir_path = os.path.dirname(os.path.realpath(__file__)) (Note that the incantation above won't work if you've already used os.chdir() to change your current working directory, since the value of the __file__ constant is relative to the current ...

  2. Open File in Another Directory (Python) - Stack Overflow

    Sep 9, 2015 · Then you can search the tuple for the directory you want and open the file in that directory: for item in o: if os.path.exists(item + '\\testfile.txt'): file = item + '\\testfile.txt' Then you can do stuf with the full file path 'file'

  3. python - How do I list all files of a directory? - Stack Overflow

    Jul 9, 2010 · Although there's a clear differentiation between file and directory terms in the question text, some may argue that directories are actually special files. The statement: "all files of a directory" can be interpreted in two ways: All direct (or level 1) descendants only. All descendants in the whole directory tree (including the ones in sub ...

  4. python - How to reliably open a file in the same directory as the ...

    On Python 3.4, the pathlib module was added, and the following code will reliably open a file in the same directory as the current script: from pathlib import Path p = Path(__file__).with_name('file.txt') with p.open('r') as f: print(f.read())

  5. python - Importing files from different folder - Stack Overflow

    Instead, run your file as a module and add a __init__.py in your some_folder directory. python -m application.app2.some_folder.some_file This will add the base directory to the path to executable python, and then classes will be accessible via a non-relative import.

  6. How can I delete a file or folder in Python? - Stack Overflow

    Aug 9, 2011 · How do I delete a file or folder in Python? For Python 3, to remove the file and directory individually, use the unlink and rmdir Path object methods respectively: from pathlib import Path dir_path = Path.home() / 'directory' file_path = dir_path / 'file' file_path.unlink() # remove file dir_path.rmdir() # remove directory

  7. Find a file in python - Stack Overflow

    Nov 12, 2009 · import subprocess def find_files(file_name): command = ['locate', file_name] output = subprocess.Popen(command, stdout=subprocess.PIPE).communicate()[0] output = output.decode() search_results = output.split('\n') return search_results search_results is a list of the absolute file paths. This is 10,000's of times faster than the methods above ...

  8. How to read a file in other directory in python - Stack Overflow

    You can't "open" a directory using the open function. This function is meant to be used to open files. Here, what you want to do is open the file that's in the directory. The first thing you must do is compute this file's path. The os.path.join function will let you do that by joining parts of the path (the directory and the file name):

  9. How do I fix the "No File or Directory issue" python?

    May 22, 2020 · The file is not found because it is looking in the current directory, which is not the same directory where your script lives. Depending on how you run Python, the current directory might be where the python executable program itself lives, or …

  10. Telling Python to save a .txt file to a certain directory on Windows ...

    Jun 28, 2012 · Python: writing file to specific directory. 1. Save a .txt file in specific path. Hot Network Questions

Refresh