I am currently working on python 3.6 using spider. I have written a code which in theory works in windows but it does not on ubuntu 18.04. My problem is that I want to write my results on a text file but it is nowhere to be found.
I write the following:
with open('Vx1.txt', 'w+') as fv1:
for itemv1 in var:
fv1.write("%s\n" % itemv1)
fv1.close()
But I am unable to find the output file Vx1.txt if there is one at all. I have tried to search from files as if I were working on a windows device and when it has not worked out I have used the console comand:
/$ find ~/ -type f -name "Vx1"
The computer does not return anything at all, just goes back to the prompt.
Can someone please tell me how can I find my output?
Please note that I have tried with single and double commas over the words when searching.
Thanks in advance.
Could it be indentation issues?
with open('Vx1.txt', 'w+') as fv1:
for itemv1 in var:
fv1.write("%s\n" % itemv1)
Related
I'm trying to change directory when calling a python file in cmd but it's not working !
I tried all types of slashes & back slashes & escaping, sometimes when the code runs, the directory isn't changing and stays the same where i start the py file and sometimes the code isn't running and i have this error Error
import os
#import sys
os.chdir('%SystemRoot%/Users/%username%/AppData/Local/Google/Chrome/User Data')
os.system('cd \"%SystemRoot%\\Users\\%username%\\AppData\\Local\\Google\\Chrome\\User Data\"')
I tried to change the system variables %SystemRoot% and %username% to words like C:/ and user2 (my system root and user name ) but still not working !
Can anyone try it in his computer and tell me what to change pls ?
Thanks !
Python does not automatically expand shell variables like %SystemRoot% and %username% to their actual values, this is what caused the error you were getting. Try os.chdir(os.path.expandvars(os.path.expanduser('%SystemRoot%/Users/%username%/AppData/Local/Google/Chrome/User Data'))) for the first line as this should expand the %username% and %SystemRoot% variables to a valid path.
EDIT: Sorry, I misunderstood your question. While this will take care of the error you were getting, you cannot change the shell's working directory from your script; see comment under your question by Ulrich Eckhardt
I am currently working on a python script in which there is a moment I want to delete a file which name is ending with .txt
To do so I just run a command line using os in python:
os.system("del working/*.txt")
When running the python script, I get the following error in cmd:
Option non valide - "*". which can be translated "Invalid option"
It seems that the wildcard isn't recognized by cmd but I know very little about this. Why is it not working ?
I know I could handle the situation with regular expressions but I'd like to understand.
Thank you in advance
In Windows, \ is the path delimiter, not /, so you should do:
os.system(r"del working\*.txt")
Note that / in Windows is for switches, hence the "invalid option" error.
I think its better use os.remove instead os.system with "del" command. Using os.system your script will not work on linux. Here a example using os.remove:
files = os.listdir("working\")
for fi in files:
if fi.endswith(".json"):
os.remove("working\{}".fomat(fi))
I have to run my python script on windows too, and then it began the problems.
Here I'm scraping html locally saved files, and then saving their .csv versions with the data I want. I ran it on my ubuntu and goes for +100k files with no problems. But when I go on windows, it says:
IOError: [Errno 13] Permission denied
It is not a permissions problems, I've rechecked it, and run it under 'Administration' powers, and it makes no difference.
It breaks exactly on the line where I open the file:
with open(of, 'w') as output:
...
I've tried to create same first file of the 100k from the python console and from a new blank stupid script from same directory as my code, and it works...
So, it seems is doable.
Then I've tried with output = open(of, 'w') instead of above code but nothing.
The weird thing is that it creates a directory with same name as the file, and then breaks with the IOError.
I've started thinking that it could be a csv thing..., naaaeehh, apart from other tries that didn't helped me, the most interesting stuff is that with the following code:
with open(of+.txt, 'w') as output:
...
it happens the astonishing thing that it creates a directory ending on .csv AND a file ending in .csv.txt with the right data!
Aargh!
Changing the open mode file to 'w+', 'wb', it didn't make a difference either.
Any ideas?
You can get permission denied if the file is opened up in another application.
Follow this link to see if any other process is using it: http://www.techsupportalert.com/content/how-find-out-which-windows-process-using-file.htm
Otherwise, I would say to try to open the file for read instead of write to see if it allows you to access it at all.
-Brian
Damn it, it's already working!, it has been like saying i cannot find my glasses and to have them on.
THanks Brian, it wasn't that the error. The problem was that in my code i was dealing with ubuntu separator besides the full path to the csv output file was completely correct. But I replaced it with os.sep , and started working like a charm :)
Thanks again!
I made this code and it is working but only in Linux.
import subprocess as sub
sub.Popen([r"Rscript","diccionari.R"])
Where "diccionari.R" is the name of my script in R.
Error text message: System can't found the specific file.
Can somebody help me and do that it works on windows please?
Thank you.
You should probably try the slashes the other way around as how I said it earlier.
Using full path to the .r script (e.g. "C:/myfolder/diccionari.R") instead of just the script file, and using OS independent slashes.
You should specify where Rscriptis located i.e
import subprocess as sub
cmd_line = [r"C:\\Program Files\\R\\R-3.6.0\\bin\\Rscript", "diccionari.R"]
sub.Popen(cmd_line)
watch for the \\ characters
I know all about how Windows uses backslashes for filenames, etc., and Unix uses forward. However, I never use backslashes with strings I create in my code. However:
When windows explorer "drops" a file onto a python script, the string it passes contains backslashes. These translate into escape sequences in the strings in the sys.argv list and then I have no way to change them after that (open to suggestions there)
Is there any way I can somehow make windows pass a literal string or ... any other way I can solve this problem?
I'd love my script to be droppable, but the only thing preventing me is windows backslashes.
EDIT:
Sorry everyone, the error was actually not the passing of the string - as someone has pointed out below, but this could still help someone else:
Make sure you use absolute path names because when the Windows shell will NOT run the script in the current directory as you would from a command line. This causes permission denied errors when attempting to write to single-part path-names that aren't absolute.
Cannot reproduce. This:
import os, sys
print sys.argv
print map(os.path.exists, sys.argv)
raw_input()
gives me this:
['D:\\workspaces\\generic\\SO_Python\\9266551.py', 'D:\\workspaces\\generic\\SO_Python\\9254991.py']
[True, True]
after dropping the second file onto the first one. Python 2.7.2 (on Windows). Can you try this code out?