Changing directory with os in python - operating-system

I'm trying to change the directory to the folder that contains the folder I'm in.
That is, I'm in /Users/ethanfuerst/Documents/Coding/mpgdata and I'm trying to go back to /Users/ethanfuerst/Documents/Coding and access a text file in the Coding folder.
When I run this code:
import os
print(os.getcwd())
os.chdir('⁨Users/ethanfuerst/Documents/Coding')
I get this output and error
/Users/ethanfuerst/Documents/Coding/mpgdata
---------------------------------------------------------------------------
FileNotFoundError Traceback (most recent call last)
<ipython-input-19-6c6ee01785f6> in <module>
1 import os
2 print(os.getcwd())
----> 3 os.chdir('⁨Users/ethanfuerst/Documents/Coding')
FileNotFoundError: [Errno 2] No such file or directory: '\u2068Users/ethanfuerst/Documents/Coding'
Does anyone know why this could be the case?
Edit: forgot to mention I am on a Mac

Try using C:\ in front of Users. If that does not work, try using .. in the directory specification to go back one directory.

Use double quotes os.chdir("⁨Users/ethanfuerst/Documents/Coding") instead of os.chdir('⁨Users/ethanfuerst/Documents/Coding')

Related

why is this showing up?

---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-1-b7eb239f86a7> in <module>
1 # Initialize path to SQLite database
2 path = 'data/classic_rock.db'
----> 3 con = sq3.Connection(path)
4
5
NameError: name 'sq3' is not defined
It looks like from the traceback that you are attempting to use sq3 and you either did not import the library or did not correctly alias the library in question. Cannot know for sure without your code though.
'sq3' is not defined
That's why. Somewhere in your code you're expecting a variable called sql3 but it doesn't exist.

Creating virtualenv inside veracypt error

I'm setting up a project inside veracrypt and it's throwing this error when I try to setup the environment.
admin#kali:/media/veracrypt1$ virtualenv --python=python3 venv
Already using interpreter /usr/bin/python3
Using base prefix '/usr'
New python executable in /media/veracrypt1/venv/bin/python3
Also creating executable in /media/veracrypt1/venv/bin/python
Traceback (most recent call last):
File "/usr/local/bin/virtualenv", line 8, in <module>
sys.exit(main())
File "/usr/local/lib/python3.7/dist-packages/virtualenv.py", line 870, in main
symlink=options.symlink,
File "/usr/local/lib/python3.7/dist-packages/virtualenv.py", line 1162, in create_environment
install_python(home_dir, lib_dir, inc_dir, bin_dir, site_packages=site_packages, clear=clear, symlink=symlink)
File "/usr/local/lib/python3.7/dist-packages/virtualenv.py", line 1672, in install_python
os.symlink(py_executable_base, full_pth)
PermissionError: [Errno 1] Operation not permitted: 'python3' -> '/media/veracrypt1/venv/bin/python'
I've tried to look for the source of the issue and it seems it's related to how it's a virtualdrive with limited rights
admin#kali:/media/veracrypt1$ ln -s testfile
ln: failed to create symbolic link './testfile': Operation not permitted
Looks like you are running this in an environment with limited permissions.
Some report this behavior when running on Linux,
but in a folder that is mounted to a "FAT32" partition -
see Chris Lope's blog-post:
permissionerror: [errno 1] operation not permitted
I have experienced this behavior while running in an Ubuntu VM
in a folder that was mounted to the host-OS (Windows-NTFS) as type 'vboxsf'.
Solved it by moving to work in a partition that is native Unix.

__import__() works with python3.6.5 but does not work with python3.7.3

The following script works fine with python3.6.5 but failed with python3.7.3 in windows 10 (home edition):
mod = __import__(pth, globals(), locals(), ['*'])
With python3.6.5 and python3.7.3, pth, globals() and locals() are almost same as follows (mPayment.py locates in scripts/demo):
pth ==> "scripts.demo.mPayment"
locals() ==> {'pth': 'scripts.demo.mPayment', 'self': <bop.bopModule.MasterModule object at 0x000002586EC3EC88>}
globals() ==> {...} #globals() has nothing to do with "scripts.demo.mPayment"
But with python3.7.3, I got exception:
Exception occurs in importing module demo.mPayment.
Traceback (most recent call last):
File "c:\xxx\b\Module.py", line 50, in _init
mod = __import__(pth, globals(), locals(), ['*'])
ModuleNotFoundError: No module named 'scripts.demo'
Anyone knows what is going on with the __import__() script in python 3.7.3 since it works with python3.6.5. Thanks a lot for your help.
ouyang
I just move the project root path in sys.path to the first one, then __import__() can find those modules.

PyPDF2 giving me an invalid argument error

I'm trying to parse text from pdf file. while I was doing tutorial of how to PyPDF2 I got the following error. I did the search for an answer but ended up finding none. Any Help will be greatly appreciated.
Traceback (most recent call last):
File "D:/text_recognizer/main.py", line 4, in <module>
inputStream = PyPDF2.PdfFileReader(input)
File "D:\KimKanna's Class\python27\lib\site-packages\PyPDF2\pdf.py", line 1084, in __init__
self.read(stream)
File "D:\KimKanna's Class\python27\lib\site-packages\PyPDF2\pdf.py", line 1689, in read
stream.seek(-1, 2)
IOError: [Errno 22] Invalid argument
here is fullcode
import PyPDF2
with open(".\\pdf\\test_sample.pdf","rb") as input:
inputStream = PyPDF2.PdfFileReader(input)
In my case the .pdf I wanted to open is empty and not closed from previous python code in powershell(cmd prompt). So, when I tried to delete those files it says 'Close the file and try again'. (that was my "AHaa" moment)
So I stopped the py.exe from my Windows task manager and deleted those empty, not closed files. Then I run the same code with another files, It worked fine.. :)

iPython notebook not uploading my .txt file

I converted some excel files to .txt files (Text (tab delimited)), carefully saved them in the same folder as my notebook, made sure there were no copies anywhere on my computer, checked the .txt files could all be opened in Notebook, and after using the command '%pylab inline' to load all the functions I usually use, triple checked that mt .txt files were in the same folder as the notebook by using the 'ls' command. I then used loadtxt('filename.txt') to load my data and it gave me errno 2: no such file or directory: 'filename.txt'. I triple checked that I had spelled everything correctly and I can't understand why it's not working. I need these data analysing today for a lab report due in tomorrow - help! Here's my code:
%pylab inline
ls
loadtxt('Physical_lab_experiment_2_25_degrees_with_times.txt')
and here is the error:
FileNotFoundError Traceback (most recent call last)
<ipython-input-9-c8d929572f25> in <module>()
----> 1 loadtxt('Physical_lab_experiment_2_25_degrees_with_times.txt')
C:\Users\Daisy\Anaconda3\lib\site-packages\numpy\lib\npyio.py in loadtxt(fname, dtype, comments, delimiter, converters, skiprows, usecols, unpack, ndmin)
738 fh = iter(open(fname, 'U'))
739 else:
--> 740 fh = iter(open(fname))
741 else:
742 fh = iter(fname)
FileNotFoundError: [Errno 2] No such file or directory: 'Physical_lab_experiment_2_25_degrees_with_times.txt'
loadtxt is a numpy function. Import numpy like:
import numpy as np
data= np.loadtxt('Physical_lab_experiment_2_25_degrees_with_times.txt')
print data