python 3.4 windows 8.1 unable to create file in C:\ - python

I'm trying to run a script in python3.4 & windows 8 .
It worked well with windows 7 but now, in windows 8.1 it can´t write a file in C: , may be access denied.
If I change the directory, for example User/xxx it works, but I don´t know how to do to 'write' in C:
with open('C:/xxxx.csv', 'r') as csvfile:
Traceback (most recent call last):
File "C:\Python34\Scripts_jmab\csv_to_shp.py", line 12, in <module>
urllib.request.urlretrieve(a,filename)
File "C:\Python34\lib\urllib\request.py", line 188, in urlretrieve
tfp = open(filename, 'wb')
OSError: [Errno 22] Invalid argument: 'C:/xxxxx.csv'
thanks

Due to security settings in Windows, users are not allowed to create files in the root C:\ folder, as some important system files reside there that could be attacked by viruses. While it is possible to disable this feature, the easiest (and safest) solution is to create a directory for your programs to put their output files in. C:\TEMP is a good location (you can create it if it doesn't already exist), or you can make another folder named according to your own choosing.

Related

open() method not working in JupyterLab ( pyspark kernel )

Getting below error when running the command:
output = open( "C:/Users/TAA3656/mytddutc_nudges_sample.json", 'w') # Update to local path and file name
[Errno 2] No such file or directory: 'C:/Users/TAA3656/mytddutc_nudges_sample.json'
Traceback (most recent call last):
FileNotFoundError: [Errno 2] No such file or directory: 'C:/Users/TAA3656/mytddutc_nudges_sample.json'
I'd guess the path C:/Users/TAA3656 doesn't exist, so it's not possible to create a file in this nonexistent path. For example:
>>> open("nonexistent/thing.json", 'w')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
FileNotFoundError: [Errno 2] No such file or directory: 'nonexistent/thing.json'
One could think that open(..., 'w') should create nonexistent/thing.json if it doesn't exist, but in this case, the directory nonexistent is... non-existent, so open refuses to create the file along with an entire path structure.
You should create the path first:
from pathlib import Path
the_path = Path("C:/Users/TAA3656")
# Create the path if it doesn't exist
the_path.mkdir(parents=True, exist_ok=True)
# Open or create the file
with (the_path / "your_file.json").open("w") as output:
... # run your code
verify the path of the file you are trying to open is correct. to verft the path exist use !pwd to show the path you are in and navegate to the location and !mkdir name to create the folder and the you can acces it.you also need to import sys and import os. so you can navegate through the system files
Do you know if you are running your jupyterlab locally, or in some kind of cloud environment? (Your mention of a pyspark kernel suggests to me it may be the latter.) If it is a cloud platform of some kind, you may not have access to your local hard disk.
Try this to check your platform, and see if it looks local to you.
Or you can try:
import os
os.getcwd()
to give you the path of where you are currently working. If it doesn't look like a directory on your local pc, again, you not be working locally.
Or ask a friendly looking colleague.

"No such file or directory" when writing to file with python

I've encountered a very bizarre error when trying to write files with python. As of today, whenever I try and open a file in write mode, python throws the error 'No such file or directory', even if the directory definitely exists. All other python functionality seems to be working, including reading files.
A simple example of the problem, when run using the command line from my documents:
>>> with open('test.csv', 'w') as f:
... pass
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
FileNotFoundError: [Errno 2] No such file or directory: 'test.csv'
I'm using a windows 10 machine, python 3.7, with conda environments specific to projects I'm working on.
I have tried:
Restarting
Creating a clean conda environment (conda create --name test python=3.7)
Running in base without a conda environment
Providing the path as an absolute rather than relative, i.e. C:/Users/<myname>/Documents/test.csv
Trying to run this in other directories
The only success I've had is when running the code with append mode. If I try and create a file as above using mode='a' it throws the error, however if I manually create an empty file, I can append into it.
I really have no idea how to resolve this, other than to completely remove anaconda and python and re-install.
try declare a variable for the file before 'with' statement:
file = open('test.csv', 'w')
with file as f:
# your code here

Accessing Sub Directories and Files Within System32 Using Python 3

I have the following code:
import os
print (os.listdir("C:/Windows/System32/config"))
print (os.listdir("C:/Windows/System32/winevt"))
I am running this Python script as an administrator and confirmed those directories in fact exist.
The first line outputs with some files and folders, but not all files. For example, there is a registry hive contained within called "SOFTWARE" which does not appear in the output.
The second line says the path wasn't identified. Here is the full output:
['Journal', 'RegBack', 'systemprofile', 'TxR']
Traceback (most recent call last):
File "test.py", line 3, in <module>
print (os.listdir("C:/Windows/System32/winevt"))
FileNotFoundError: [WinError 3] The system cannot find the path specified: 'C:/Windows/System32/winevt'
This module (os.listdir), works pretty much everywhere else on the system, but not within System32. I suspect this may be permission related, but am not too sure on how to fix it as I am already running the script as an administrator.
Help would be appreciated. Thanks!

Python numpy.savetxt - no such file or directory, although path is correct

Found no solution here on stackoverflow yet. In the threads I read, where this problem is considered, the problem was triggered by a wrong path. But in my case, the path should be correct, since I copied it to the windows navigation line and it worked.
The error message looks as follows:
Traceback (most recent call last):
File "C:\Users\micha\Dropbox\10. Semester\Fabios_error_bound\python_new_pc\feb_error_calculation_and_plotting\feb_error_calculation_and_plotting\feb_error_calculation_and_plotting.py", line 99, in <module>
np.savetxt(os.path.join(working_directory,error_folder,current_name.replace('.txt','')+'error_pop11.txt'),error_pop11, fmt='%20.14f')
File "C:\Python27amd64\lib\site-packages\numpy\lib\npyio.py", line 1194, in savetxt
fh = open(fname, 'w')
IOError: [Errno 2] No such file or directory: 'C:\\Users\\micha\\Dropbox\\10. Semester\\Fabios_error_bound\\python_new_pc\\feb_error_calculation_and_plotting\\feb_error_calculation_and_plotting\\error2\\ses_pop_site_eb__ohmic_lam50_gam100_T100_SD_ohmic_lam50_gam100_T100_angle39_Chl658_Egap0_J100_M2_K0_L8error_pop11.txt'
Furthermore, it works on my PC, but not on my notebook. In both cases I'm using Visual Studio with Python 3.5 and 3.6, respectively (with 2.7 it also doesn't work on my Notebook).
Does anybody has a clue?

Opening a file in Python gives me errors

I am quite new to Python and I am having problems opening a file in Python.
I want to open a text file called 'coolStuff' in a Folder on my Desktop and this is how I type in the command but I still get an error message. The file exists and so I do not understand why I get that error message.
open("coolStuff.txt","r")
Traceback (most recent call last):
File "<pyshell#11>", line 1, in <module>
open("coolStuff.txt","r")
FileNotFoundError: [Errno 2] No such file or directory: 'coolStuff.txt'
If you want to simply supply a filename like coolStuff.txt without also providing a full directory, then you have to make sure that Python is running in the same directory as the file. If you aren't sure what directory Python is running in, try this:
import os
print(os.getcwd())
You have two options:
let's say your file is in C:\path\to\dir\coolStuff.txt
1.
open(r'C:\path\to\dir\coolStuff.txt','r')
2.
import os
os.chdir(r'c:\path\to\dir')
open('coolStuff.txt', 'r')
Because the file you want to open is not in the current directory.You can find the file 'coolStuff.txt' in the terminal and launch your python environment at the same directory.

Categories