trash = "~/.Trash"
new_folder = []
for current_file in os.listdir(trash):
new_folder.append(current_file)
The above code is not working. I am trying to move files from the trash to a folder in python on a Mac. The error message I'm getting is
FileNotFoundError: [Errno 2] No such file or directory: '~/.Trash'
Expanding the ~ is a feature of your shell. Your can not use it like a normal file system path. Luckily Python has a function to assist you with that:
trash = os.path.expanduser('~/.Trash')
See https://docs.python.org/3/library/os.path.html#os.path.expanduser for details.
Related
I use subprocess.popen in python to put files in hdfs. it runs accurately using python on the Windows cmd. but as I use vscode to run the code, I get "FileNotFoundError: [Errno 2] No such file or directory: Error.
hdfs_path = os.path.join(os.sep,'mongo_import_export')
#put csv into hdfs
put = Popen(['hadoop','dfs','-put','mongo-hadoop/import_export.csv','/mongo_import_export'], stdin=PIPE,bufsize=-1)
put.communicate()
Knowing that my file import_export.csv is in the file in witch the code is located and mong-hadoop folder is in my local files
VSCode is running the code in a different working directory than your local CMD. Use the absolute path to the files you want to put rather than relative paths.
I am trying to scrape tables from pdf with read_pdf in python. I am using read_pdf but it doesn't do the job. Also, to mention, I do this in MAC with Jupiter notebook.
This is what I do:
from tabula import read_pdf
file = read_pdf(r'C:\Users\myname\Rprojects\Reports_scraping\data_scraped\icnarc_29052020\icnarc_200529.pdf')
I get this error:
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\myname\\Rprojects\\Reports_scraping\\data_scraped\\icnarc_29052020\\icnarc_200529.pdf'
How I can solve this issue?
just to check that the file exist, do you get True when running this:
import os
file_path = r'C:\Users\myname\Rprojects\Reports_scraping\data_scraped\icnarc_29052020\icnarc_200529.pdf'
print( os.path.isfile(file_path))
Edit file_path with wherever is the file(using Python 3).
And did you change "myname" in the path with your actual username... (just in case)
It is preferable to build your paths using os.path.join to make things compatible, on windows it will need to create a root "config.py" file, see
how to get the root folder on windows
#
having discussed with GaB, it seemed that he is using Jupyter notebook on Mac, which explains issues, I saw this link, but can't help more.
Jupyter - import pdf
os.path.join doc
There can be only one possibility, the file is not there, but you have already checked that I assume, if not, Once again check whether the spelling of file is correct. If this doesn't work, then do below trick
Execute the py code in same folder as file, and then use
from tabula import read_pdf
file = read_pdf(r'icnarc_200529.pdf')
Sometimes, this simple method does the trick.
When i run in terminal i get the [Errno 2] No such file or directory error, but when i use debug mode it works.
the error occurs here,
list = open(filename, "r")
the code also works with IDLE
Your debugger is most likely running in a different location than your terminal. cd your terminal to where the file is located, or try using the absolute path instead of a relative one.
If you add these few lines in your code and run it using your IDE or Terminal, you'll notice the difference:
import os
curdir = os.getcwd()
print('My current directory is {}'.format(curdir))
fullpath_to_filename = os.path.join(curdir, filename)
print('When I run `open(filename)`, python sees: {}'.format(fullpath_to_filename))
print('This filepath is {}valid'.format('' if os.path.exists(fullpath_to_filename) else 'not '))
You'll likely experience something similar to below:
# on IDE:
My current directory is c:\Users\me\Projects\
When I run `open(filename)`, python sees: c:\Users\me\Projects\test.txt
This filepath is valid
# on Terminal:
My current directory is c:\Programs\Python38\
When I run `open(filename)`, python sees: c:\Programs\Python38\test.txt
This filepath is not valid
Solution: use absolute path for your filename, or os.chdir(...) to the correct parent path before location filename. I would recommend the former to avoid managing directories.
I just try to create some new folders with Python's (3.7.3) os.makedirs() and os mkdir().
Apparently, it works fine because no error occurs (Win 10 Home). But as I try to find the created folder in the windows explorer it isn't there.
Trying to create it again with python I get an error:
'[WinError 183] Cannot create a file when that file already exists:'
Strange thing is, all this worked fine on my computer at work (Wind 10 too), and also on my android tablet.
I've already tried to use relative & absolute paths in makedirs / mkdir.
Ok, it's all about these few lines:
import os
# print(os.getcwd()) shows: C:\Users\Andrej\Desktop
# tried relative path..
os.makedirs('Level1/Level2/Level3')
# tried out some absolute paths like...
os.makedirs('C:/Users/Andrej/Desktop/Level1/Level2/Level3')
os.makedirs('C:\\Users\\Andrej\\Desktop\\Level1\\Level2\\Level3')
UPDATE: It works perfectly when I write makedirs command directly in the Windows powershell. The issue above only occurs when I write this code in Visual Code Studio and then start the file from the powershell by typing in: python makedirs.py...
I just had the same thing happen to me, except I was creating a folder in the AppData/Roaming directory and was using the version of Python from the Microsoft Store.
Apparently, files and directories created in AppData using that version of Python, will instead be created in:
%LocalAppData%\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache
I found what causes this problem finally!
All of the created test folders have been moved to a folder named VTROOT. This folder is created by Comodo Internet Security I use...Its "Auto-Containment" moved all folders I set up by running the code from .py-file (only then) in the PowerShell.
Disabling the auto-containment of Comodo solves the problem..Oh my..
Thank you anyways!
the "if" part will check if the file exists already and consequentially solve the ERROR: '[WinError 183] Cannot create a file when that file already exists:'
the "import sys" part will fix the issue with windows where the folder is created but not visible by the user (even with show hidden files turned on)
import sys
import os
path = 'your path'
def make_dir():
if not os.path.exists(Path):
os.makedirs(Path)
or
import sys
import os
path = 'your path'
def make_dir():
mode = 0o666 #read and write for all users and groups
# form more permission codes search for: *chmod number codes*
if not os.path.exists(Path):
os.mkdir(Path, mode)
Try adding the below import statement:
import sys
This should work.
I'm trying to open a file in python. Simple enough. The script that I am using is the same directory as my code, so I just use
myfile = open('file.txt', 'r')
This worked fine before, but now I am getting the error 'No such file or directory' (Errno2)
Why is this happening? I've used OS to check if I am in the right directory, and it is fine. What is python doing differently now than it did 20 minutes ago, when it found the file perfectly??
Assuming the file you are trying to open/read has appropriate permissions, the behavior is defined based on how you are invoking your python program. Let's assume your code and the file.txt are in ~/Desktop
If you are in ~/Desktop and do a python code.py your code will work fine. But if you are in say your home folder - ~ and do a python ~/Desktop/code.py then the python interpreter assumes your current working directory to be ~ and will return the error:
IOError: [Errno 2] No such file or directory: 'file.txt'
since it will not find file.txt in ~
Further, in the context of the given example:
os.getcwd()
returns the absolute path of your home directory and
os.path.realpath(__file__)
returns the absolute path of your python source file
Is it possible you are typing the name wrong, eg "test.fna" versus "test.fna.txt"?