質問編集履歴

6

追加

2022/11/08 14:11

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -83,3 +83,41 @@
83
83
  ```
84
84
  回答をもとにf = openからのコードを変更してみました。
85
85
  相変わらずpath部分にInvalid argumentエラーが出てしまいます。
86
+
87
+ 追記4
88
+ ```Python
89
+ import os
90
+
91
+ with open('E:/VI/list.txt') as f:
92
+ file_readlines = f.readlines()
93
+
94
+ for json_file in file_readlines:
95
+ if json_file.startswith('apple'):
96
+ path = 'E:/VI/' + json_file 
97
+ with open(path, mode="w") as reader: #Invalid argument
98
+ content = reader.read()
99
+
100
+ content = content.replace("label_index: 5", "label_index: 1")
101
+
102
+ with open('1apple.json', mode="w") as writer:
103
+ writer.write(content)
104
+ ```
105
+
106
+ 新しくファイルを作成して書き込むようコードを変更してみました。
107
+ 「E:/VI/」に変更してみたもののInvalid argumentのままでした。
108
+ 試しに、
109
+ ```Python
110
+ print(path)
111
+ ```
112
+ を実行したところ、
113
+ E:/VI/apple-001.json
114
+
115
+ E:/VI/apple-002.json
116
+
117
+ E:/VI/apple-003.json
118
+
119
+ E:/VI/apple-004.json
120
+
121
+ E:/VI/apple-005.json
122
+
123
+ が出力されたため、ファイルは確かにあるのですが、上手くいきません。

5

修正

2022/11/08 11:32

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -76,7 +76,7 @@
76
76
  with open(path) as reader: #Invalid argument
77
77
  content = reader.read()
78
78
 
79
- content = content.replace("label_index: 0", "label_index: 1")
79
+ content = content.replace("label_index: 5", "label_index: 1")
80
80
 
81
81
  with open(path) as writer:
82
82
  writer.write(content)

4

修正

2022/11/08 11:30

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -73,7 +73,7 @@
73
73
  for json_file in file_readlines:
74
74
  if json_file.startswith('apple'):
75
75
  path = 'E:/VI' + json_file 
76
-    with open(path) as reader: #Invalid argument
76
+ with open(path) as reader: #Invalid argument
77
77
  content = reader.read()
78
78
 
79
79
  content = content.replace("label_index: 0", "label_index: 1")

3

追記

2022/11/08 11:28

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -62,3 +62,24 @@
62
62
  回答をもとに書き換えましたが、上記の認識で合っているでしょうか。
63
63
  このコードで実行したところInvalid argumentとエラーが出てしまいました。(’E:/VI/apple001.json’)
64
64
  しかし、ファイルを開いて確認したところ"label_index: 0"に書き換えられていました。
65
+
66
+ 追記3
67
+ ```Python
68
+ import os
69
+
70
+ with open('E:/VI/list.txt') as f:
71
+ file_readlines = f.readlines()
72
+
73
+ for json_file in file_readlines:
74
+ if json_file.startswith('apple'):
75
+ path = 'E:/VI' + json_file 
76
+    with open(path) as reader: #Invalid argument
77
+ content = reader.read()
78
+
79
+ content = content.replace("label_index: 0", "label_index: 1")
80
+
81
+ with open(path) as writer:
82
+ writer.write(content)
83
+ ```
84
+ 回答をもとにf = openからのコードを変更してみました。
85
+ 相変わらずpath部分にInvalid argumentエラーが出てしまいます。

2

追記

2022/11/08 08:36

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -43,3 +43,22 @@
43
43
 
44
44
  f.close()
45
45
  ```
46
+
47
+ 追記2
48
+ ```Python
49
+ import os
50
+
51
+ with open('E:/VI/list.txt') as f:
52
+ file_readlines = f.readlines()
53
+
54
+ for json_file in file_readlines:
55
+ if json_file.startswith('apple'):
56
+ path = 'E:/VI' + json_file
57
+ f = open(path)
58
+ data_lines = data_lines.replace("label_index: 5", "label_index: 1")
59
+ f.close()
60
+ ```
61
+
62
+ 回答をもとに書き換えましたが、上記の認識で合っているでしょうか。
63
+ このコードで実行したところInvalid argumentとエラーが出てしまいました。(’E:/VI/apple001.json’)
64
+ しかし、ファイルを開いて確認したところ"label_index: 0"に書き換えられていました。

1

コードの追加

2022/11/08 06:35

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -23,3 +23,23 @@
23
23
  ファイル自体の書き換えを行うコードは出てくるのですが、具体的にどのような命令を書けばいいかアドバイスをいただきたいです。
24
24
  よろしくお願いいたします。
25
25
 
26
+ 追記
27
+ 返信欄のコードでは、ファイル名が直接代入されていました。
28
+ 以下、ファイル名のリストを代入するコードに変更しましたが、同様のエラーが出ています。
29
+
30
+ ```Python
31
+ import os
32
+
33
+ with open('E:/VI/list.txt') as f:
34
+ file_readlines = f.readlines()
35
+
36
+ str =file_readlines
37
+
38
+
39
+ if str.startswith('apple'):
40
+ path = 'E:/VI'
41
+ f = open(path)
42
+ data_lines = data_lines.replace("label_index: 5", "label_index: 1")
43
+
44
+ f.close()
45
+ ```