blender can't read the .txt file - python

I'm trying to draw a cube from the co-ordinates stored in text file it's working just fine when I'm executing it in vs code but my blender text editor keeps giving me 'No such file or directory' error
with open("list.txt", "r") as filestream:
for line in filestream:
currentline = line.split(",")
total = str( int(currentline[0]) + int(currentline[1]) + int(currentline[2])) + "\n"
print(currentline)[enter image description here][1]

Just provide the absolute path to the txt file, because blender python shell may not have its working directory in the directory containing the txt file.
EDIT
You could try to import the os module and call os.chdir("/directory/containing/the/txt/file/") before opening the txt file.

Related

Trouble finding file directory using Chrome OS

I'm trying to read in a .txt file on chromebook to manipulate the data that is there. My code to source the file is:
def readInFile():
arr_intValues = []
myFile = open("#MY FILE HERE", "r")
#you need to change the directory
for myLine in myFile:
arr_intValues.append(int(myLine))
return arr_intValues
myNewList = readInFile()
print(myNewList)
Trouble is, i cannot find out where the source of the file is for this code. If i drop the file into a chrome tab, it reads:
file:///media/fuse/crostini_9549350604ce9beeb7d5a9a66d5bf09733144d34_termina_penguin/RandomNum.txt
Meanwhile the file location that "Get info" returns is:
My files/Linux files/RandomNum.txt
Both of these options fail if I attempt to open it and print it in my code.
How do I find the correct file directory?
I attempted to find the directory of a .txt file on using Chrome OS and have not been successful.
If you're running the program on Linux, you'll need to specify the Linux path.

errno 2 no such file or directory from command line file in python

I am getting that error for my program with my sys.argv[1] file. The specific csv file i am importing is there i can see it and open it myself.
myfile = open("sys.argv[1]", "r")
reader = csv.DictReader(myfile)
dnalist = list()
for dictionary in reader:
dnalist.appeand(dictionary)
It cant seem to find the file I input into my command line. I double checked that I am in the correct folder and spell the name of the subfolder correctly and the file name.

file line writing closes off script

I'm looking to make a file that when you enter a directory, it creates a folder inside that directory. I'm using a batch file to create the folder, so I'm making it when you enter the directory, it will write that directory to the batch file, then run it. However I'm having some errors. Here's my code
directory = str(input())
text = 'cd ' + directory
lines = open('new.bat', 'r').readlines()
lines[2] = text
out = open('new.bat', 'w')
out.writelines(lines)
out.close()
call('new.bat')
out.close() exits my python script before it can have a chance to call 'new.bat', however if I move the out.close() to after it calls new.bat, it gives me an error that the file is currently being used. How do I fix this?

I want to process every file inside a folder line by line and get a particular matching string

I am trying to process every files inside a folder line by line. I need to check for a particular string and write into an excel sheet. Using my code, if i explicitly give the file name, the code will work. If I try to get all the files, then it throws an IOError. The code which I wrote is as below.
import os
def test_extract_programid():
folder = 'C://Work//Scripts//CMDC_Analysis//logs'
for filename in os.listdir(folder):
print filename
with open(filename, 'r') as fo:
strings = ("/uri")
<conditions>
for line in fo:
if strings in line:
<conditions>
I think the error is that the file is already opened when the for loop started but i am not sure. printing the file name prints the file name correctly.
The error shown is IOError: [Errno 2] No such file or directory:
if your working directory is not the same as folder, then you need to give open the path the the file as well:
with open(folder+'/'+filename, 'r') as fo
Alternatively, you can use glob
import glob
for filename in glob.glob(folder+'/*'):
print filename
It can't open the path. You should do
for filename in os.listdir(folder):
print folder+os.sep()+filename

Opening all files in a directory for CSV and reading each line Python

Hi I am looking for some help with regards to a problem I am having. I want to search the directory that my data is in (shown below) for only certain file types. Below is my code but it is not quite functioning.
The current output is one of two results. Either it prints just the first line of the file, just prints blank result.
OK so here is what I want to do. I want to search the directory listed for only csv files. Then what I want to do is to get the loop to read each file line by line and print each line in the file and then repeat this for the rest of the csv files.
Can anyone please show me how to edit the code below to search only for CSV files and also how to print each line that is in the file and then repeat for the next CSV file untill all the CSV files are found and opened. Is this possible?
import os
rootdir= 'C:\Documents and Settings\Guest\My Documents\Code'
def doWhatYouWant(line):
print line
for subdir, dirs, files in os.walk(rootdir):
for file in files:
f=open(file,'r')
lines = f.readlines()
f.close()
f=open(file,'wU')
for lines in lines:
newline=doWhatYouWant(line)
f.write(newline)
f.close
Thanks for your help.
This code below works. I have commented what were modified inline.
import os
rootdir= 'C:\\Documents\ and\ Settings\\Guest\\My\ Documents\\Code\\'
#use '\\' in a normal string if you mean to make it be a '\'
#use '\ ' in a normal string if you mean to make it be a ' '
def doWhatYouWant(line):
print line
return line
#let the function return, not only print, to get the value for use as below
for subdir, dirs, files in os.walk(rootdir):
for file in files:
f=open(rootdir+file,'r') #use the absolute URL of the file
lines = f.readlines()
f.close()
f=open(file,'w') #universal mode can only be used with 'r' mode
for line in lines:
newline=doWhatYouWant(line)
f.write(newline)
f.close()

Categories

Resources